简体   繁体   English

有人可以在方法内部解释这种行为吗?

[英]Can someone explain This behaviour inside of method?

Hi Beginner here so sorry for any ignorance if I showed.嗨初学者在这里很抱歉,如果我展示了任何无知。

const test = {
    myfunction(){
        console.log(this);
    },
    myfunction3(){
        function myfunction4(){
            console.log(this)
        }
        return myfunction4()
    } }

and when I run当我跑步时

test.myfunction3()

I receive global object as a window.我收到全局 object 作为 window。 I am a little bit confused how this happened.我有点困惑这是怎么发生的。 My question is我的问题是

  1. myfunction3() can access to myfunction4() because of its hierarchy? myfunction3()可以访问myfunction4()因为它的层次结构? If so, is there anyway I can access to myfunction4() directly instead of going through myfunction3()?如果是这样,我是否可以直接访问myfunction4()而不是通过 myfunction3()?
  2. Why this in myfunction4() returned global window instead of a reference to myfunction4() ?为什么this myfunction4()中的 this 返回全局 window 而不是对myfunction4()的引用?

Thank you for your help!谢谢您的帮助!

In JavaScript, essentially all your functions are treated as objects and hence the reason why you able to access myfunction4 by calling via myfunction3.在 JavaScript 中,基本上您的所有函数都被视为对象,因此您可以通过 myfunction3 调用访问 myfunction4。

this , inside any of the javaScript function, will be the object on which the function is invoked. this在 javaScript function 中的任何一个内,将是 object 调用 ZC1C425268E68385D14 的 object。 So, by default this refer to global object, in your browser, it is the window object.因此,默认情况下, this指的是全局 object,在您的浏览器中,它是 window object。

https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Basics#what_is_this https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Basics#what_is_this

1. You cannot access myfunction4 in such method of "test.myfunction3.myfunction4()" 1. “test.myfunction3.myfunction4()”这种方法不能访问myfunction4

Because myfunction4 is not property of myfunction3 and especially, test.myfunction3 is not object.因为 myfunction4 不是 myfunction3 的属性,尤其是 test.myfunction3 不是 object。

2. Since myfunction4 is not method, instead it is function, "this" refer Window object in myfunction4. 2.由于myfunction4不是方法,而是function,“this”指的是myfunction4中的Window object。

"this" in method refer the parent object but in function, it refer global object like Window.方法中的“this”指的是父 object,但在 function 中,它指的是全局 object,例如 ZC89686A3835D2B12BCEB3BCA27。

The reason you're getting the global object has to do with your function call/invocation and not with the function placement itself.您获得全局 object 的原因与您的 function 调用/调用有关,而不是与 function 放置本身有关。

Invocation, I'm referring to ==> test.myFunction3()调用,我指的是 ==> test.myFunction3()

Object methods often times are a bit confusing, but just because a function sits inside of another function means nothing. Object 方法通常有点令人困惑,但仅仅因为 function 位于另一个 function 内部就没有任何意义。 It's not about placement of a function but its invocation.. so here what matters is test.myFunction3()这不是关于放置 function 而是它的调用.. 所以这里重要的是 test.myFunction3()

Hope it helps.希望能帮助到你。

myfunction3() can access to myfunction4() because of its hierarchy? myfunction3() 可以访问 myfunction4() 因为它的层次结构? If so, is there anyway I can access to myfunction4() directly instead of going through myfunction3()?如果是这样,我是否可以直接访问 myfunction4() 而不是通过 myfunction3()?

You can't access the function directly through myFunction3() , but you can return a reference to it, which you can then use.您不能直接通过myFunction3()访问 function ,但可以返回对它的引用,然后可以使用它。 See below where I point out that you have a typo in your implementation of myFunction3()请参阅下文,我指出您在执行myFunction3()时有错字

const func4 = myFunction3();

func4();

Why this in myfunction4() returned global window instead of a reference to myfunction4()?为什么 myfunction4() 中的 this 返回全局 window 而不是对 myfunction4() 的引用?

There are two issues, first this in a function is bound at runtime.有两个问题,首先是 function 中的this在运行时绑定。

Second, because of typo in myFunction3() you are returning the result of calling myFunction4 rather than returning a reference to the function itself, which I assume is what you are trying to do.其次,由于myFunction3()中的拼写错误,您返回的是调用myFunction4的结果,而不是返回对 function 本身的引用,我认为这是您正在尝试做的。

myfunction3(){

    function myfunction4(){
        console.log(this)
    }
    return myfunction4; // this returns the function, rather than return the result of calling the function (drop the () )
}

Perhaps you just needed to convert it into an object for the function call to work like this:也许您只需要将其转换为 object 调用 function 就可以像这样工作:

var name = 'global';   // when 'this' scope is global

var test = {
    name: 'local',   // when 'this' scope is local to test
    fun1: function() { //same fun as an object
        console.log(this.name);
    },
    fun3: function() {
        function myfunction4(){
            console.log(this.name);
        }
        return myfunction4()
    } 
}

And if you wanted to call the functions in the same way you were doing it above, then you could it have done so like this:如果您想以与上面相同的方式调用函数,那么您可以这样做:

test.fun1();     // local

test.fun3();    // global (that's what inside of fun4) 

You cannot call fun4 directly but can findout the value by calling fun3.您不能直接调用 fun4,但可以通过调用 fun3 找出值。

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

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