在 JavaScript 中的对象内部分配柯里化函数时,this 和 self 之间有什么区别吗?

Is there any difference between this and self when assigning curried functions inside an object in JavaScript?

假设我有以下代码:

<html>
<head>

    <title>test</title>
</head>
<body>
    <header><h1>test</h1></header>

    <script type="text/javascript">
        function myFunction2Wrapper(arg1) {
            return function() {
                console.log("state of arg1 in a curried function is: " + arg1);
            }
        }

        function MyObject() {
            var internalState1 = "a";

            function myFunction1() {
                console.log("state of internalState1: " + internalState1);
            }
            myFunction1();

            this.myFunction2 = myFunction2Wrapper(internalState1);
            //self.myFunction2 = myFunction2Wrapper(internalState1);

            myFunction2();

            //console.log(myFunction2);

            console.log("done!");

        };

        MyObject();



    </script>

</body>
</html>

请特别注意以下几行:

this.myFunction2 = myFunction2Wrapper(internalState1);
//self.myFunction2 = myFunction2Wrapper(internalState1);

我的问题是:在 JavaScript 中的对象内部分配柯里化函数时,thisself 之间有什么区别吗?

没有self隐式context/parameter。在任何函数中使用的 self 实际上是指 window.self 等于 window,而不是 this.

注意:在全局范围函数中,this 指的是 window

我猜你在示例代码中使用 thisself 的想法不清楚,self 通常用于 cotext 更改时。例如:

function MyClass(){
   this.element = 'some';
   this.anotherElement = []; 
}

//now references of the properties of MyClass be with self
MyClass.prototype.myMethod = function(e){
    self = this;
    self.element.length;
    self.anotherElement.push('4');
};

在 JS 中,由于 this 的松散和好玩的状态,thatself 短语是开发人员最喜欢的短语,以在某些时候包含 this 值他们算法的阶段。然而,应该避免使用 self,因为与 that 不同,self 是 JS 中的一个关键字,它代表每个 web worker 域的全局上下文。由于 worker 上下文中的全局范围被称为 self 并作为 self 访问,因此不鼓励将 self 关键字用于其他目的。