简体   繁体   English

JavaScript内部函数和“此”

[英]JavaScript Inner Functions & 'this'

In my understanding, it is not until an object invokes a Function that this is actually assigned a value. 在我的理解,它是不是直到有一个对象调用, 实际上是分配一个值的函数。 And the value it is assigned is based exclusively on the object that invokes the Function. 并且为其分配的值完全基于调用该功能的对象。

Also, the scope chain rule in JS is LEG. 另外,JS中的范围链规则是LEG。

So, in (strict mode): 因此,在(严格模式)下:

function one () {
    var a = 2;
    function two () {
        console.log(a)};
    two()
}

one() // output 2

But: 但:

function one () {
    var a = 2;
    function two () {
        console.log(this.a)};
    two()
}

one()  //output undefined

It was my understandig functions were objects, and in the previous invokation the function object one would be the making the call of two , which translates this.a into one.a . 这是我understandig功能为对象,在以前的invokation函数对象之一将是使该两个电话,这转化成this.a one.a。 Obviously that is not the case. 显然不是这样。

Also: 也:

function one () {
        var a = 2}

console.log(one.a)  //outputs undefined

Any clarification about what is happening would be very appreciated. 关于正在发生的任何澄清将不胜感激。

Thanks 谢谢

function one () {
    var a = 2;
    function two () {
        console.log(this.a)};
    two()
}

one()  //output undefined

Here you are calling both one and two as functions on their own, not as properties of some object (eg someObject.one() ). 在这里,您将onetwo称为函数,而不是某个对象的属性(例如someObject.one() )。 This means that this will refer to the global scope (or to undefined if the code is in strict mode). 这意味着, this将是指全球范围内(或到undefined如果代码是在严格模式下)。 The a property of your global scope is undefined , so that's why you see undefined . 全局范围的a属性是undefined ,所以这就是为什么您看到undefined的原因。 Calling two() inside of one() doesn't make it so that this refers to one . 调用two()one()这样不会使this是指one

function one () {
        var a = 2}

console.log(one.a)  //outputs undefined

a is not a property of one . a是没有的属性one It is a variable inside it . 这是它里面的变量。 A property of one would look like this. 的属性one是这样的。

 function one() { } one.a = 7; console.log(one.a); 

I think you are treating a regular function as a class object. 我认为您正在将常规函数视为类对象。 You only call one() but you do not actually instantiate a new One() object. 您仅调用one(),但实际上并未实例化新的One()对象。

Scope and Context are different concepts. 范围上下文是不同的概念。 Within a JS-Function scope one can address any value that either got assigned inside a function or any value that got assigned outside such a function as long as this function itself is a member of the outer scope. 在JS-Function范围内,只要该函数本身是外部范围的成员,就可以处理在函数内部分配的任何值或在该函数外部分配的任何值。 In addition, as long as a function does not create a closure, scope gets created just at a function's call time. 另外,只要函数不创建闭包, 作用域就在函数的调用时创建。 Context is addressable too. 上下文也是可寻址的。 But unlike scope , context gets evaluated at a functions call time. 但是与scope不同, 上下文在函数调用时进行评估 Thus this is just a placeholder for context that can vary from one time calling a function to another. 因此, this只是上下文的占位符,可以从一次调用一个函数到另一个调用一次而有所不同。

 function contextAwareMethod(injectedValue) { "use strict"; var context = this, // dynamically evaluated. innerScopeValue = "created at a functions call time"; console.log('dynamically evaluated context : ', context); console.log('this.contextValue : ', (context && context.value)); console.log('innerScopeValue : ', innerScopeValue); console.log('injectedValue : ', injectedValue); } console.log('"case A"'); contextAwareMethod('argument A'); console.log('\\n'); console.log('"case B"'); contextAwareMethod.call({ value: "conext B"}); console.log('\\n'); console.log('"case C"'); contextAwareMethod.call({ value: "conext C"}, 'argument C'); console.log('\\n'); 
 .as-console-wrapper { max-height: 100%!important; top: 0; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM