有没有替换Javascript构造函数的.prototype而不是添加到它的问题? [英] Are there problems with replacing a Javascript constructor's .prototype rather than adding to it?

查看:154
本文介绍了有没有替换Javascript构造函数的.prototype而不是添加到它的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到另一个开发人员的代码,这样做类似这样定义一个Javascript类型:

I've come across another developer's code which does something like this to define a Javascript type:

function Ninja() {}
Ninja.prototype = {
  swingSword: function(){
    return true;
  }
}

p>

when the more conventional manner is something like:

function Ninja() {}
Ninja.prototype.swingSword = function() {
    return true;
}

第一种形式主要工作,因为你可以创建一个新的实例, swingSword
方法和 instanceof 是准确的:

The first form mostly works, in that you can create a new instance and call the swingSword method, and instanceof is accurate:

var ninja = new Ninja();
ninja.swingSword(); // returns true
ninja instanceof Ninja; // also true

然而, ninja.constructor 指向 Object ,而不是 Ninja 函数。这对我来说是一个问题:我试图通过一个实例访问构造函数,作为构造函数不以任何其他方式暴露的解决方法。但除了访问反射/解决方法黑客的构造函数属性,第一种形式是否导致任何重大问题?

However, ninja.constructor points to Object rather than the Ninja function. This is an issue for me: I'm trying to access the constructor via an instance, as a workaround for the constructor not being exposed in any other way. But apart from accessing the constructor attribute for reflection/workaround hacks, does the first form cause any significant issues?

有没有任何其他重要属性的原始 Ninja.prototype 可能会被隐藏?第一种形式会导致继承类型或常用库的任何问题吗?

Are there any other important attributes of the original Ninja.prototype which might be getting hidden? Will the first form cause any issues with inheriting types, or common libraries?

我试图找出这是一个不寻常但可以接受的Javascript模式,

I'm trying to figure out if this an unusual but acceptable Javascript pattern, or actually a common Javascript misstep or "gotcha" and should be considered a bug.

注意我的 Ninja 示例是基于一些<从John Resig的教程中获得一个href =http://ejohn.org/apps/learn/#65 =nofollow>示例代码。所讨论的实际类型不是 Ninja ,而是更长更复杂。

Note my Ninja example is based on some example code from a tutorial by John Resig. The actual type in question is not called Ninja, and is longer and more complicated.

阅读指南,了解构造函数和原型,并了解,例如如何在属性查找中使用原型,并且为什么 isinstanceof 仍然有效。

Also, I've read this guide to constructors and prototypes and understand, eg how prototypes are used in attribute lookups and why isinstanceof still works.

推荐答案

当你从另一个原型继承你通常替换整个原型,如果构造函数是重要的,你可以重新设置为正确值:

When you "inherit" from another prototype you usually replace the entire prototype, if constructor is important you can re set it to the "correct" value:

var Child = function(){};
//"inherit" from Parent
Child.prototype=Object.create(Parent.prototype);
//reset the constructor
Child.prototype.constructor = Child;

所以在你的情况下,只需修复构造函数,你设置原型:

So in your case just repair the constructor after you set the prototype:

var Ninja = function() {}
Ninja.prototype = {
  swingSword: function(){
    return true;
  }
};
// you can put the next line anywhere you want to fix the problem
// http://stackoverflow.com/a/16063711/1641941 under this.constructor
Ninja.prototype.constructor=Ninja;

或:

var Ninja = function() {}
Ninja.prototype = {
  swingSword: function(){
    return true;
  },
  constructor:Ninja
};

这篇关于有没有替换Javascript构造函数的.prototype而不是添加到它的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆