javascript this 关键字 for study
来源:互联网 发布:js 扫描枪录入 编辑:程序博客网 时间:2024/04/29 19:23
Unlike variables, the this keyword does not have a scope, and nested functions do not
inherit the this value of their caller. If a nested function is invoked as a method, its
this value is the object it was invoked on. If a nested function is invoked as a function
then its this value will be either the global object (non-strict mode) or undefined (strict
mode). It is a common mistake to assume that a nested function invoked as a function
can use this to obtain the invocation context of the outer function. If you want to access
the this value of the outer function, you need to store that value into a variable that is
in scope for the inner function. It is common to use the variable self for this purpose.
For example:
var o = { // An object o.
m: function() { // Method m of the object.
var self = this; // Save the this value in a variable.
console.log(this === o); // Prints "true": this is the object o.
f(); // Now call the helper function f().
function f() { // A nested function f
console.log(this === o); // "false": this is global or undefined
console.log(self === o); // "true": self is the outer this value.
}
}
};
inherit the this value of their caller. If a nested function is invoked as a method, its
this value is the object it was invoked on. If a nested function is invoked as a function
then its this value will be either the global object (non-strict mode) or undefined (strict
mode). It is a common mistake to assume that a nested function invoked as a function
can use this to obtain the invocation context of the outer function. If you want to access
the this value of the outer function, you need to store that value into a variable that is
in scope for the inner function. It is common to use the variable self for this purpose.
For example:
var o = { // An object o.
m: function() { // Method m of the object.
var self = this; // Save the this value in a variable.
console.log(this === o); // Prints "true": this is the object o.
f(); // Now call the helper function f().
function f() { // A nested function f
console.log(this === o); // "false": this is global or undefined
console.log(self === o); // "true": self is the outer this value.
}
}
};
o.m(); // Invoke the method m on the object o.
来自[JavaScript权威指南(第6版)].(JavaScript:The.Definitive.Guide).David.Flanagan
- javascript this 关键字 for study
- Java study 05day-this关键字
- JavaScript this 关键字
- Javascript中的this关键字
- JavaScript中的this关键字
- javascript this 关键字浅析
- JavaScript 中的this 关键字
- JavaScript之this关键字
- javascript的this关键字
- JavaScript的this关键字
- Javascript中的关键字this
- javascript this关键字
- javascript this关键字
- Javascript this关键字说明
- javascript this 关键字
- JavaScript中的this关键字
- Javascript中的this关键字
- JavaScript this关键字
- 杭电ACM 2054 A == B ?
- mysql中文全文索引
- SAMATE - Software Assurance Metrics And Tool Evaluation
- HttpWebResponse请求状态代码
- 有一种心痛叫移民
- javascript this 关键字 for study
- 野火图片
- Category:OWASP Best Practices: Use of Web Application Firewalls/Version 1.0.5
- Oracle APEX 在页面直接传递参数值
- jQuery防止重复提交的方法
- Java基础第十三天学习日记_String字符串
- Python学习笔记一 基础知识
- 你会“学习”吗
- HDU_3709 Balanced Number 数位dp