Javascript call() & apply() vs bind()?

I already know that apply and call are similar functions which set this (context of a function). The difference is with the way we send the arguments (manual vs array) Question: But when should I use the bind() method ? var obj = { x: 81, getX: function() { return this.x; } }; alert(obj.getX.bind(obj)()); alert(obj.getX.call(obj)); alert(obj.getX.apply(obj)); jsbin Use .bind(

Javascript调用()&apply()vs bind()?

我已经知道apply和call是设置this类似函数(函数的上下文)。 区别在于我们发送参数的方式(手动vs数组) 题: 但是什么时候应该使用bind()方法? var obj = { x: 81, getX: function() { return this.x; } }; alert(obj.getX.bind(obj)()); alert(obj.getX.call(obj)); alert(obj.getX.apply(obj)); jsbin 当您希望稍后使用特定上下文来调用该函数时,可以使用.bind() ,这在事件中很有用。 当想要立即调

! preceding function in javascript?

Possible Duplicate: What does the exclamation mark do before the function? I saw a function formatted like this today for the first time: !function(){}(); What is the preceding exclamation mark for? I assume it functions the same as: (function(){})(); But... what's going on here? The preceding ! takes the un-parseable statement, and allows it to to be parsed by the JS engine, whic

! 前面的函数在javascript中?

可能重复: 该功能之前感叹号做了什么? 我今天第一次看到这样格式化的函数: !function(){}(); 前面的感叹号是什么? 我认为它的功能与以下相同: (function(){})(); 但是......这里发生了什么? 前面! 采用不可解析的语句,并允许它由JS引擎解析,而JS引擎则反过来返回true。 function(){}(); SyntaxError: Unexpected token ( !function(){}(); >>true 它只是使JavaScript解析器将其解析为一个表达式,这

What is the purpose of the HTML "no

I notice that in a lot of template engines, in the HTML5 Boilerplate, in various frameworks and in plain php sites there is the no-js class added onto the <HTML> tag. Why is this done? Is there some sort of default browser behavior that reacts to this class? Why include it always? Does that not render the class itself obsolete, if there is no no-"no-js" case and html can be a

什么是HTML的目的“没有

我注意到在很多模板引擎中,在HTML5 Boilerplate中,在各种框架和普通的php站点中, no-js类添加到<HTML>标记中。 为什么这样做? 是否有某种对此类反应的默认浏览器行为? 为什么总是包括它? 如果没有no-“no-js”的情况并且html可以直接处理,那么这不会使类本身过时吗? 以下是HTML5 Boilerplate index.html中的一个示例: <!--[if lt IE 7 ]> <html lang="en" class="no-js ie6"> <![endif]-->

What is the purpose of backbone.js?

I tried to understand the utility of backbone.js from its site http://documentcloud.github.com/backbone, but I still couldn't figure out much. Can anybody help me by explaining how it works and how could it be helpful in writing better JavaScript? Backbone.js is basically an uber-light framework that allows you to structure your Javascript code in an MVC (Model, View, Controller) fashion

backbone.js的目的是什么?

我试图从它的网站http://documentcloud.github.com/backbone了解backbone.js的效用,但我仍然无法弄清楚。 任何人都可以通过解释它的工作原理来帮助我,它如何能够帮助我编写更好的JavaScript? Backbone.js基本上是一个超级明亮的框架,它允许你以MVC (模型,视图,控制器)方式构建你的Javascript代码,其中...... Model是检索和填充数据的代码的一部分, 视图是此模型的HTML表示(视图随模型更改等而变化) 和可选

What is the purpose of Node.js module.exports and how do you use it?

What is the purpose of Node.js module.exports and how do you use it? I can't seem to find any information on this, but it appears to be a rather important part of Node.js as I often see it in source code. According to the Node.js documentation: module A reference to the current module . In particular module.exports is the same as the exports object. See src/node.js for more informat

Node.js module.exports的用途是什么?你如何使用它?

Node.js module.exports的用途是什么?你如何使用它? 我似乎无法找到关于此的任何信息,但它似乎是Node.js的一个相当重要的部分,因为我经常在源代码中看到它。 根据Node.js文档: 模 对当前module引用。 特别是module.exports与exports对象相同。 有关更多信息,请参阅src/node.js 但这并没有真正的帮助。 module.exports做了什么,一个简单的例子是什么? module.exports是作为require调用的结果实际返回的对

Why do Chrome and Firefox handle javascript variable set inside jQuery ajax() callback differently?

Using jQuery 1.9.1, calling back to the server to check some data: $form = $("#form2") var str = $form.serialize(); status = true; $.ajax({ type : 'POST', url : 'check_zip.php', data : str, async : false, success : function (data) { obj = JSON.parse(data); var result = obj.result;

为什么Chrome和Firefox处理jQuery ajax()回调中的javascript变量集的方式不同?

使用jQuery 1.9.1,回调服务器来检查一些数据: $form = $("#form2") var str = $form.serialize(); status = true; $.ajax({ type : 'POST', url : 'check_zip.php', data : str, async : false, success : function (data) { obj = JSON.parse(data); var result = obj.result; status = res

Why JavaScript says that a number is not a number?

This question already has an answer here: Why does typeof NaN return 'number'? 20 answers As I understand it, NaN is a sentinel instance of the Number class that represents, well, exactly what it stands for - numeric results that cannot be adequately represented. So 0/0 is not a number, in the sense that it's NaN , but it is a Number in terms of its type. Perhaps it should hav

为什么JavaScript说数字不是数字?

这个问题在这里已经有了答案: 为什么typeof NaN返回'数字'? 20个答案 据我了解, NaN是Number类的一个哨兵实例,它恰好代表了它所代表的数值结果 - 无法充分表示的结果。 所以0/0不是一个数字,因为它是NaN ,但是它的类型是一个Number 。 也许它应该被称为NaRN(不是可表示数字)。 如果您有一个变量并将其分配给0/0的结果,该变量仍然是数值类型,但该值未定义(不是数字)。 还有其他条件可能发生,但这

Javascript binding event handler function by name

Possible Duplicate: JavaScript: var functionName = function() {} vs function functionName() {} Why does this work... $("#clickme").click( showAlert ); function showAlert() { alert( "Hiya" ); } ... but not this...? $("#clickme").click( showAlert ); var showAlert = function() { alert( "Hello" ); } This is happening due to hoisting. In your first case, the code is interpreted as (

Javascript绑定事件处理函数的名字

可能重复: JavaScript:var functionName = function(){} vs function functionName(){} 为什么这个工作... $("#clickme").click( showAlert ); function showAlert() { alert( "Hiya" ); } ...但不是这个...? $("#clickme").click( showAlert ); var showAlert = function() { alert( "Hello" ); } 这是由于吊装发生的。 在你的第一种情况下,代码被解释为(注意如何首先评估函数声明): function sh

What is the point of using a named function expression?

This question already has an answer here: var functionName = function() {} vs function functionName() {} 31 answers Why use named function expressions? 5 answers Some people prefer to do it like this because if errors occur, your functions have names. It's mostly a matter of preference and how often you have trouble with unnamed functions. You don't normally see it used in a var

使用命名函数表达式有什么意义?

这个问题在这里已经有了答案: var functionName = function(){} vs function functionName(){} 31个答案 为什么使用命名函数表达式 5个答案 有些人喜欢这样做,因为如果发生错误,你的功能就有名字。 这主要是一个偏好问题,您经常遇到未命名函数的问题。 您通常不会看到它在var声明中使用,而是在声明回调时: callbackFunction(function success() { ... }, function fail() { ... }) 这样你就知道哪个参数是

Why window.addEventListener does not work?

This question already has an answer here: var functionName = function() {} vs function functionName() {} 31 answers While you using var x = function(){} to declare functions, it should be declare before you call the function. By function x (){} , x will exist in current scope, no matter how later it declare. Because such side effect , the var x = function(){} are more recommended. For exa

为什么window.addEventListener不起作用?

这个问题在这里已经有了答案: var functionName = function(){} vs function functionName(){} 31个答案 当您使用var x = function(){}来声明函数时,应该在调用函数之前声明它。 通过function x (){} , x将存在于当前作用域中,无论它在多久后声明。 因为这样的副作用 ,更推荐var x = function(){} 。 例如: var user = 'alien'; if( user == 'alien') { function salute () { console.log("Welcome to Ear