This question already has an answer here: JavaScript plus sign in front of function name 3 answers That is so that the function declaration is a function expression, so that it can be executed immediately. Usually that is done by putting parentheses around it: (function ($) { "use strict"; //... }(window.jQuery)); or: (function ($) { "use strict"; //... })(window.jQuery);
这个问题在这里已经有了答案: JavaScript加上函数名称前面的3个答案 这是因为函数声明是一个函数表达式,所以它可以立即执行。 通常这是通过在其中放置括号来完成的: (function ($) { "use strict"; //... }(window.jQuery)); 要么: (function ($) { "use strict"; //... })(window.jQuery);
This question already has an answer here: JavaScript plus sign in front of function name 3 answers It is normally used with IIFE/SIFE. When you use + sign like that, it evaluates the expression following that, so when you put it in a function, it executes even an anonymous function, like this +function(){ console.log("Welcome"); }() Output Welcome It is another way to get the same be
这个问题在这里已经有了答案: JavaScript加上函数名称前面的3个答案 它通常与IIFE / SIFE一起使用。 当你使用+符号的时候,它会根据它来评估表达式,所以当你把它放在一个函数中时,它甚至会执行一个匿名函数,就像这样 +function(){ console.log("Welcome"); }() 产量 Welcome 当整个函数用圆括号括起来时,这是另一种获得相同行为的方法,就像这样 (function(){ console.log("Welcome"); }()); 注意 :不
I have two functions. When enter is pressed the functions runs correctly but when escape is pressed it doesn't. What's the correct number for the escape key? $(document).keypress(function(e) { if (e.which == 13) $('.save').click(); // enter (works as expected) if (e.which == 27) $('.cancel').click(); // esc (does not work) }); 尝试使用关键事件: $(document).keyup(function(
我有两个功能。 当按下Enter键时,功能运行正常,但按下转义键时不会。 退出键的正确数字是什么? $(document).keypress(function(e) { if (e.which == 13) $('.save').click(); // enter (works as expected) if (e.which == 27) $('.cancel').click(); // esc (does not work) }); 尝试使用关键事件: $(document).keyup(function(e) { if (e.keyCode === 13) $('.save').click(); // enter if (e.keyC
Is there a way to call inc of obj without bind? function callIt(callback){ callback(); } var obj = { count: 0, inc: function(){ this.count++; } }; callIt(obj.inc.bind(obj)); obj.count;// 1 Question is relevant to me because of older IE-versions that do not support the method bind. 你可以使用一个函数值callIt(function() { obj.inc(); });
有没有一种方法可以在不绑定的情况下调用obj的公司? function callIt(callback){ callback(); } var obj = { count: 0, inc: function(){ this.count++; } }; callIt(obj.inc.bind(obj)); obj.count;// 1 问题与我有关,因为不支持方法绑定的较旧的IE版本。 你可以使用一个函数值callIt(function() { obj.inc(); });
Is there any way to get something like the following to work in JavaScript? var foo = { a: 5, b: 6, c: this.a + this.b // Doesn't work }; In the current form, this code obviously throws a reference error since this doesn't refer to foo . But is there any way to have values in an object literal's properties depend on other properties declared earlier? Well, the only thing
有没有什么办法让下面的东西在JavaScript中工作? var foo = { a: 5, b: 6, c: this.a + this.b // Doesn't work }; 在当前的形式中,这段代码显然会引发一个引用错误,因为this并不涉及foo 。 但是有没有办法让对象的值取决于前面声明的其他属性? 那么,我可以告诉你的唯一的事情就是获得者: var foo = { a: 5, b: 6, get c () { return this.a + this.b; } }; foo.c; // 11 这是ECMAScript
// I don't understand why this is not working y = 'window'; var x = { y : 'x', func : function(){ return function(){ return this.y } } }; x.func()(); // when I execute x.func()() I understand why it should return y // x.func() would return a function in the global context and then execute it ' should return 'window' ' y = 'window' var x = { y : '
//我不明白为什么这不起作用 y = 'window'; var x = { y : 'x', func : function(){ return function(){ return this.y } } }; x.func()(); // when I execute x.func()() I understand why it should return y // x.func() would return a function in the global context and then execute it ' should return 'window' ' y = 'window' var x = { y : 'x', func : function
I am trying to navigate to another page after user is authenticated with angularfire. Everything works except navigating to another page. Here is my code: constructor(public navCtrl: NavController, public menu: MenuController, public afAuth: AngularFireAuth, public db: AngularFireDatabase, private platform : Platform) { this.navigateIfUserIsLogdIn(); } navigateIfUserIsLogdIn(){
我正在尝试在用户使用angularfire进行身份验证后导航到另一个页面。 除了导航到其他页面之外,一切都可以工作。 这是我的代码: constructor(public navCtrl: NavController, public menu: MenuController, public afAuth: AngularFireAuth, public db: AngularFireDatabase, private platform : Platform) { this.navigateIfUserIsLogdIn(); } navigateIfUserIsLogdIn(){ this.authState = this.afAuth.
Given following JavaScript code: ({ foo: 1, bar: 2, zoo: 3, test: function(i) { var g = function(i) { alert(i + zoo); } g(i); } }).test(2); Why is zoo undefined in alert() ? Which kind of syntax can I use to correctly reference to zoo and get alert display for value 5 ? Update: I would prefer solution where only implementation of g needs to change, if at all pos
鉴于以下JavaScript代码: ({ foo: 1, bar: 2, zoo: 3, test: function(i) { var g = function(i) { alert(i + zoo); } g(i); } }).test(2); 为什么zoo在alert()未定义? 我可以使用哪种语法来正确引用zoo并获得值为5警报显示? 更新:如果可能的话,我更喜欢只需要改变g实现的解决方案。 zoo不是一个自由浮动变量,它是对象的一个属性。 在test你可以使用this参考对象(因为你调用它的
I am trying to have a mouse hover event where the circle radius gets bigger, and the corresponding data label increases font size. The data labels are on the circle, in the graph itself. So my idea was to have two functions, one to style the circles nice and smooth with a transition, and use a separate function with tweening to style the text. I want to call both functions at the same time and
我正在尝试在鼠标悬停事件中圆圈半径变大,并且相应的数据标签会增加字体大小。 数据标签位于图形本身的圆上。 所以我的想法是有两个功能,一个用圆滑的方式来设置圆,并使用一个单独的函数进行文本的样式修改。 我想同时调用两个函数,并让它们只有数据绑定与'id'匹配的样式对象。 Id只是我从.tsv解析的索引。 这个想法是文字和圆都有相同的'ID'。 !boring circle svg code above .on('mouseenter', funct
This question already has an answer here: How does the “this” keyword work? 19 answers Because value of this is determined by how function is called.. closure is called with no reference of context and global context is window (in Browser) Use Function.prototype.call to specify this context while function is invoked var car = { brand: "Nissan", getBrand: function() { var closur
这个问题在这里已经有了答案: “this”关键字如何工作? 19个答案 因为this值取决于function的调用方式。 closure被调用时没有引用context ,全局上下文是window (在浏览器中) 使用Function.prototype.call在invoked Function.prototype.call时指定this上下文 var car = { brand: "Nissan", getBrand: function() { var closure = function() { console.log(this.brand); console.log(this);