he scope of all functions is window.

(The reason why is you are invoking f as a function(类,全局的类) and not a method. When invoked as a function this is set to window during the execution of the target)

To circumvent that, you can do this:

A.prototype.go = function() {
var self = this;
console.log(self); //A { go=function()}
var f = function() {
console.log(self); //A { go=function()}
}; f();
}

  this would only not refer to the window when it is in the method of a class and not just a regular function. –

看一段有意思的代码,猜测一下,弹出的 this 变量会是什么?

function Menu(elem) {
alert(this);
function privateMethod() {
alert(this) // window, not menu!
} // ... call private method
privateMethod()
} new Menu(document.createElement('div'))

  

Because function f() is not called without any object reference. Try,

f.apply(this);
function Menu(elem) {
alert(this);
function privateMethod() {
alert(this) // window, not menu!
} // ... call private method
privateMethod.apply(this)
} new Menu(document.createElement('div'))

  ——---------------------------------------------------------------------------------------------------------------------------

参考 stack-overflow:

http://stackoverflow.com/questions/9674252/javascript-why-this-inside-the-private-function-refers-to-the-global-scope

http://javascript.info/tutorial/binding

JavaScript has a different concept of what the special name this refers to than most other programming languages do. There are exactly five different ways in which the value of this can be bound in the language.

The Global Scope

this;

When using this in global scope, it will simply refer to the global object.

Calling a Function

foo();

Here, this will again refer to the global object.

ES5 Note: In strict mode, the global case no longer exists. this will instead have the value of undefined in that case.

Calling a Method

test.foo();

In this example, this will refer to test.

Calling a Constructor

new foo();

A function call that is preceded by the new keyword acts as a constructor. Inside the function, thiswill refer to a newly created Object.

Explicit Setting of this

function foo(a, b, c) {}

var bar = {};
foo.apply(bar, [1, 2, 3]); // array will expand to the below
foo.call(bar, 1, 2, 3); // results in a = 1, b = 2, c = 3

When using the call or apply methods of Function.prototype, the value of this inside the called function gets explicitly set to the first argument of the corresponding function call.

As a result, in the above example the method case does not apply, and this inside of foo will be set to bar.

Note: this cannot be used to refer to the object inside of an Object literal. So var obj = {me: this} will not result in me referring to obj, since this only gets bound by one of the five listed cases.

Common Pitfalls

While most of these cases make sense, the first one is to be considered another mis-design of the language because it never has any practical use.

Foo.method = function() {
function test() {
// this is set to the global object
}
test();
}

A common misconception is that this inside of test refers to Foo; while in fact, it does not.

In order to gain access to Foo from within test, it is necessary to create a local variable inside of method which refers to Foo.

Foo.method = function() {
var that = this;
function test() {
// Use that instead of this here
}
test();
}

that is just a normal variable name, but it is commonly used for the reference to an outer this. In combination with closures, it can also be used to pass this values around.

Assigning Methods

Another thing that does not work in JavaScript is function aliasing, which is assigning a method to a variable.

var test = someObject.methodTest;
test();

Due to the first case, test now acts like a plain function call; therefore, this inside it will no longer refer to someObject.

While the late binding of this might seem like a bad idea at first, in fact, it is what makes prototypal inheritance work.

function Foo() {}
Foo.prototype.method = function() {}; function Bar() {}
Bar.prototype = Foo.prototype; new Bar().method();

When method gets called on a instance of Barthis will now refer to that very instance.

Disclaimer: Shamelessy stolen from my own resources at http://bonsaiden.github.com/JavaScript-Garden/#function.this

最新文章

  1. python征程3.0(python对象)
  2. .Net开源网络爬虫Abot介绍
  3. Spring Security笔记:使用BCrypt算法加密存储登录密码
  4. Launcher2编译
  5. 走进Groovy (二)
  6. ubuntu 14.04安装postgresql最新版本
  7. ASP.NET中的Response
  8. c++(排序二叉树插入)
  9. Unity 读取资源(图片)
  10. BZOJ4943 [NOI2017] 蚯蚓
  11. 996.ICU 写给... 写给年轻的自己
  12. 关于php,python,javascript文件或者模块导入引入的区别和联系
  13. algorithm与numeric的一些常用函数
  14. Zabbix监控Nginx性能状态
  15. [20171220]toad plsql显示整形的bug.txt
  16. jmeter 中 Client implementation HttpClient4和java区别实践一
  17. 【WPF】附加属性
  18. centos6.8 安装python2.7 or python3.6
  19. ZooKeeper在线迁移
  20. 2017 计蒜之道 初赛 第五场 C. UCloud 的安全秘钥(中等)

热门文章

  1. IO模式设置网络编程常见问题总结—IO模式设置,阻塞与非阻塞的比较,recv参数对性能的影响—O_NONBLOCK(open使用)、IPC_NOWAIT(msgrcv)、MSG_DONTWAIT(re
  2. cordova环境配置步骤
  3. sudo su– user
  4. 【jsp网站计数功能】 application session
  5. java获取程序执行时间
  6. Zookeeper: configuring on centos7
  7. [转]探究java IO之FileInputStream类
  8. 2016.10.08--Intel Code Challenge Final Round--D. Dense Subsequence
  9. 1--OC -- HelloWorld
  10. ExtJS3.4升级ExtJS4.2的问题汇总(转)