简体   繁体   English

如何使用原型更新胖箭头功能?

[英]How to update fat arrow function using prototype?

Is there a way in which we may update fat arrow function using prototype ? 有没有一种方法可以使用prototype更新粗箭头功能?

class Greeter {
    greeting: string;
    constructor(message: string) {
        this.greeting = message;
    }
    greet = () => {
        alert("Hello, " + this.greeting + " inside");
    }
}

Greeter.prototype.greet = () => {
    alert("Hello, " + this.greeting + " outside");
}

let greeter = new Greeter("world");
greeter.greet();
// It should alert Hello world outside
// But it is showing  Hello world inside

Am i understanding fat arrow syntax incorrectly, and it can not be updated like this, any reference would be helpful? 我是否错误地理解了胖箭头语法,并且无法像这样进行更新,所以任何参考都将有所帮助吗?

Here is the Link for Typescript Playground where I was trying 这是我正在尝试的Typescript Playground的链接

Thanks in Advance!! 提前致谢!!

When you create a Greeter object. 创建Greeter对象时。 It sets: 它设置:

this.greet = function () {
  alert("Hello, " + _this.greeting + "inside");
};

So it will override what you set in prototype. 因此它将覆盖您在原型中设置的内容。 If you don't write the same greet() function, then it will call what you set in prototype. 如果您没有编写相同的greet()函数,则它将调用您在原型中设置的内容。

Another thing to remember is that when you use arrow function: 要记住的另一件事是,当您使用箭头功能时:

Greeter.prototype.greet = () => {
  alert("Hello, " + this.greeting + " outside");
}

this. here is not the object itself, but it belongs to the outer context, that means you can't get this.greeting correctly. 这里不是对象本身,而是属于外部上下文,这意味着您无法正确地获得this.greeting。

To understand what's going on better you should look at the compiled js code: 要了解发生了什么事情,您应该查看已编译的js代码:

var _this = this;
var Greeter = (function () {
    function Greeter(message) {
        var _this = this;
        this.greet = function () {
            alert("Hello, " + _this.greeting + " inside");
        };
        this.greeting = message;
    }
    return Greeter;
}());
Greeter.prototype.greet = function () {
    alert("Hello, " + _this.greeting + " outside");
};

There is a greet function which is added to the prototype, but then when you create a new instance of Greeter this method is being overridden in the constructor. 原型中添加了一个greet函数,但是当您创建Greeter的新实例时,该方法将在构造函数中被覆盖。

It's usually better to have normal class methods: 通常,最好使用普通的类方法:

class Greeter {
    // ...
    greet() {
        alert("Hello, " + this.greeting + " inside");
    }
}

Which then compiles into js with the alert already on the prototype: 然后将其编译为带有原型中已经存在的alert js:

var Greeter = (function () {
    function Greeter() {
    }
    Greeter.prototype.greet = function () {
        alert("Hello, " + this.greeting + " inside");
    };
    return Greeter;
}());

If you worry about losing the scope of this when executing greet (because you pass it as a callback or something similar) then it's easy to just: 如果你担心失去的范围this在执行时greet (因为你把它作为一个回调或类似的东西),那么它很容易只是:

setTimeout(greeter.greet.bind(greeter), 1000);

Or 要么

setTimeout(() => { greeter.greet(); }, 1000);

Edit 编辑

If you want to do something like this: 如果您想做这样的事情:

class Greeter {
    greeting: string;
    message: string = "";
    constructor(message: string) {
        this.greeting = message;
    }
    greet(){
      return "Hello, " + this.greeting + " inside";
    }
}

let oldGreet = Greeter.prototype.greet;
Greeter.prototype.greet = function(){
   return oldGreet() + " appended outside";
}

It won't work as it will print: 它无法工作,因为它将打印:

"Hello, undefined inside appended outside" “您好,未定义的内部附加在外部”

And that's because the this isn't the instance of Greeter , it should be oldGreet.call(this) : 那是因为this不是Greeter的实例,应该是oldGreet.call(this)

Greeter.prototype.greet = function(){
   return oldGreet.call(this) + " appended outside";
}

( code in playground ) 操场上的代码

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

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