bind()还有一个功能:将除了第一个用于绑定this的参数之外的其他参数传给下层的函数(部分应用,是“柯里化”的一种)。 这里涉及到一个概念:把null或者undefined作为this的绑定对象传入call、apply、bind,这些值在调用的时候会被忽略,实际应用默认绑定规则。 应用场景: 1、使用apply()展开一个数组,并作为参数传递给一...
f2()=== undefined;//true 在严格模式下,this 是在进入运行环境时设置的。若没有定义,this的值将维持undefined状态。同时它也能设置成任意值,比如 null 或者42 或者“I am not this”。 注意:在第二个例子中,this应该是undefined。因为f2不是作为方法调用的(见下文)。这个功能并未在所有第一次开始支持严格模...
log(this === undefined); // => true return a * b; } // multiply() function invocation with strict mode enabled // this in multiply() is undefined multiply(2, 5); // => 10 当使用严格模式调用 multiply(2, 5) 的时候,this 就是undefined。 严格模式不仅对当前作用域有效,它内部的作用...
f2() === undefined; //return undefined 在严格模式下,this的值根据执行时的上下文,this所保存的值决定。若为定义,this仍是undefined, 它可能被设置为任何的值,比如null,42或者是 " I'am not this "。 在第二个例子中,this的值应该是undefined。因为f2被调用时未基于任何对象(e.g.window.f2())。这个功...
当使用严格模式调用multiply(2, 5)的时候,this就是undefined。 严格模式不仅对当前作用域有效,它内部的作用域(当前作用域内定义的函数内)也会是严格模式。 代码语言:javascript 代码运行次数:0 运行 AI代码解释 functionexecute(){'use strict';functionconcat(str1,str2){// the strict mode is enabled tooconso...
calculate()是函数调用(但不是方法调用),因此这里this是全局对象window(案例2.1。)或undefined处于严格模式下(案例2.2。)。即使外部函数sum()将上下文作为numbers对象,它在这里也没有影响。 的调用结果numbers.sum()为NaN(或TypeError: Cannot read property 'numberA' of undefined在严格模式下引发错误)。绝对不是预...
In a browser window the global object is[object Window]: Example functionmyFunction() { returnthis; } Try it Yourself » thisin a Function (Strict) JavaScriptstrict modedoes not allow default binding. So, when used in a function, in strict mode,thisisundefined. ...
getThisStrict()); // "number" 如果函数在没有被任何东西访问的情况下被调用,this 将是undefined——但只有在函数处于严格模式下会如此。jsCopy to Clipboard console.log(typeof getThisStrict()); // "undefined" 在非严格模式下,一个特殊的过程称为 this 替换确保this 的值总是一个对象。这意味着:...
二、This 四种绑定规则 1. 默认绑定 当函数调用属于独立调用(不带函数引用的调用),无法调用其他的绑定规则,我们给它一个称呼 “默认绑定”,在非严格模式下绑定到全局对象,在使用了严格模式 (use strict) 下绑定到 undefined。 严格模式下调用: 复制
getThisStrict()); // "number" 如果函数在没有被任何东西访问的情况下被调用,this 将是undefined——但只有在函数处于严格模式下会如此。jsCopy to Clipboard console.log(typeof getThisStrict()); // "undefined" 在非严格模式下,一个特殊的过程称为 this 替换确保this 的值总是一个对象。这意味着:...