javascript 中的函数调用总是一个方法吗
Are function calls in javascript always a method
Douglas Crockford 教了我们很多关于 js 的知识。特别是有 4 种使用函数的方法
-
功能应用
function f() {}
f();
-
方法调用
var obj = {f: function (){}};
obj.f();
-
应用模式
var obj = {};
function f(){}
f.apply(obj);
-
构造函数调用
function Person(){}
new Person();
我的问题是使用Function应用时的this参数是全局对象。不就是一个特殊的Method Inovcation吗
例如
function f(){}
this.f(); // acts exactly like calling f();
好像真的只有3种模式。函数应用实际上并不存在。
It seems there are really only 3 patterns. Function Application doesn't actually exist.
确实如此,因为 f();
使用默认值 this
即使 this
在您 运行 的上下文中它不是默认值 this
.
例如:
var global = this;
var obj = {
method: function() {
console.log(this === global); // false
f();
function f() {
console.log(this === global); // true
}
}
};
obj.method();
换句话说:您的断言仅在全球范围内为真。
我在上面说 "the default this
" 是因为默认值 this
会根据您处于严格模式还是松散模式而有所不同。在严格模式下,它是 undefined
。在松散模式下,它是对全局对象的引用。
Isn't it effectivly just a special Method Inovcation.
反过来说:JavaScript doesn't have methods。它具有 函数 ,以及调用将 this
设置为各种值的函数的方法。 其中一种 设置 this
来引用带有函数引用的 属性 来自的对象(例如,obj.foo()
)。
function f(){}是在作用域内创建的,所以真的很像方法调用。
因此,如果您在 window space 中执行此操作,则实际上创建的是 window.f()。因此,当它指向当前名称 space 即 window 时,您可以通过 this.f() 访问它。但是,如果您在其他函数中创建了一个新函数,则无法通过 this 直接访问它,但您必须先获取对该对象的引用。
JavaScript 没有方法 - 只有函数。每个函数调用都有一个上下文 (this
),它(通常)在运行时决定,而不是函数固有的 属性。
"Method" 风格的调用不多于或少于一种使用显式 this
调用函数的方法。以下调用具有完全相同的效果:
function f() { }
var o = { f: f }
o.f();
f.call(f);
o.f.call(f);
事实上 f
可以 被称为 o
的 "method" 与 f
没有特殊关系或它的调用方式。
函数应用使用默认this
,在严格模式下是undefined
或者全局对象(window
或global
) 否则。
N.B。可以使用 Function.prototype.bind
:
将特定的 this
烘焙到函数中
var g = f.bind(o);
g(); // `this` is o, not the global object.
Douglas Crockford 教了我们很多关于 js 的知识。特别是有 4 种使用函数的方法
- 功能应用
function f() {}
f();
- 方法调用
var obj = {f: function (){}};
obj.f();
- 应用模式
var obj = {};
function f(){}
f.apply(obj);
- 构造函数调用
function Person(){}
new Person();
我的问题是使用Function应用时的this参数是全局对象。不就是一个特殊的Method Inovcation吗
例如
function f(){}
this.f(); // acts exactly like calling f();
好像真的只有3种模式。函数应用实际上并不存在。
It seems there are really only 3 patterns. Function Application doesn't actually exist.
确实如此,因为 f();
使用默认值 this
即使 this
在您 运行 的上下文中它不是默认值 this
.
例如:
var global = this;
var obj = {
method: function() {
console.log(this === global); // false
f();
function f() {
console.log(this === global); // true
}
}
};
obj.method();
换句话说:您的断言仅在全球范围内为真。
我在上面说 "the default this
" 是因为默认值 this
会根据您处于严格模式还是松散模式而有所不同。在严格模式下,它是 undefined
。在松散模式下,它是对全局对象的引用。
Isn't it effectivly just a special Method Inovcation.
反过来说:JavaScript doesn't have methods。它具有 函数 ,以及调用将 this
设置为各种值的函数的方法。 其中一种 设置 this
来引用带有函数引用的 属性 来自的对象(例如,obj.foo()
)。
function f(){}是在作用域内创建的,所以真的很像方法调用。
因此,如果您在 window space 中执行此操作,则实际上创建的是 window.f()。因此,当它指向当前名称 space 即 window 时,您可以通过 this.f() 访问它。但是,如果您在其他函数中创建了一个新函数,则无法通过 this 直接访问它,但您必须先获取对该对象的引用。
JavaScript 没有方法 - 只有函数。每个函数调用都有一个上下文 (this
),它(通常)在运行时决定,而不是函数固有的 属性。
"Method" 风格的调用不多于或少于一种使用显式 this
调用函数的方法。以下调用具有完全相同的效果:
function f() { }
var o = { f: f }
o.f();
f.call(f);
o.f.call(f);
事实上 f
可以 被称为 o
的 "method" 与 f
没有特殊关系或它的调用方式。
函数应用使用默认this
,在严格模式下是undefined
或者全局对象(window
或global
) 否则。
N.B。可以使用 Function.prototype.bind
:
this
烘焙到函数中
var g = f.bind(o);
g(); // `this` is o, not the global object.