简体   繁体   English

在构造函数中定义原型方法

[英]Defining prototype methods inside the constructor

Today, I saw a JavaScript pattern I have never seen in my whole life. 今天,我看到了一生中从未见过的JavaScript模式。 I cannot tell the purpose of using this pattern. 我无法说出使用这种模式的目的。 It seems wrong to me, but I want to be a little conservative. 这对我来说似乎不对,但我想保守一点。 It might be some awesome pattern I never saw before. 这可能是我以前从未见过的一些很棒的模式。

function Dog() {
    Dog.prototype.bark = function () {
        alert('woof!');
    }

    this.bark = function () {
        Dog.prototype.bark();
    }

    this.bark();
}

First, I'm not a fan for making methods (as privileged members) inside the constructor for no reason. 首先,我不是没有理由在构造函数中创建方法(作为特权成员)的粉丝。 It would cause creating functions every time when an instance is created. 每次创建实例时都会导致创建函数。 Second, in this code snippet, it also calls the prototype name "Dog", instead of "this". 其次,在这段代码片段中,它还调用原型名称“Dog”,而不是“this”。 This makes me super confused. 这让我非常困惑。

Anyone knows what good about it? 谁知道它有什么好处?

Thanks! 谢谢! Grace 恩典

This is a very bad idea, for a great number of reasons. 出于很多原因,这是一个非常糟糕的主意。 A few of which are: 其中一些是:

  1. Adding methods to the prototype in the constructor will cause the prototype method to be replaced for all instances, everytime you instantiate a new Dog. 在构造函数中向原型添加方法将导致每次实例化一个新Dog时替换所有实例的prototype方法。
  2. Calling Dog.prototype.bark() means that this will be Dog.prototype and not your instance of Dog , which can cause serious issues. 调用Dog.prototype.bark()意味着this将是Dog.prototype而不是你的Dog实例,这可能会导致严重的问题。
  3. this.bark = function () { Dog.prototype.bark(); } this.bark = function () { Dog.prototype.bark(); } is some serious WTF. this.bark = function () { Dog.prototype.bark(); }是一些严重的跆拳道。 Because this.bark will already evaluate to the prototype method making this unnecessary. 因为this.bark已经评估了原型方法,因此不必要。 And calling it like this actually destroys the natural this value, as mentioned in #2. 并且像这样调用它实际上会破坏自然的this值,正如#2中提到的那样。

Here is what is should be: 这应该是:

function Dog() {
  this.makeSound();
};

Dog.prototype.bark = function() {
  alert('woof');
};

Dog.prototype.makeSound = function() {
  this.bark();
};

Or alternatively, without the prototype at all: 或者,根本没有原型:

function Dog() {
  this.bark = function() {
    alert('woof');
  };

  this.makeSound = function() {
    this.bark();
  };

  this.makeSound();
};

I would not trust this snippet of yours at all. 我根本不相信你的这个片段。

Sorry for the VERY late reply, but if you really want to add the prototypes within the constructor AND not have it recreated whenever a new instance is created, you could do this: 很抱歉非常迟到的回复,但是如果你真的想在构造函数中添加原型并且在创建新实例时没有重新创建原型,那么你可以这样做:

function Dog() {
    if (!Dog.prototype.bark) {
        Dog.prototype = function() {
            console.log('woof');
        }
    }

    this.bark();
}

I still probably wouldn't use this method, but I have some across instances where this method was a option when dealing with 3rd party frameworks (the dodgy kind). 我仍然可能不会使用这种方法,但我有一些跨实例,在处理第三方框架(狡猾的类型)时这个方法是一个选项。

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

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