Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JavaScript中使用所需参数对类进行子类化_Javascript_Oop - Fatal编程技术网

在JavaScript中使用所需参数对类进行子类化

在JavaScript中使用所需参数对类进行子类化,javascript,oop,Javascript,Oop,如果JavaScript中的“类”子类化是这样完成的: var ParentClass = function() { // something }; var ChildClass = function() { // something }; ChildClass.prototype = new ParentClass(); 。。。当父类具有必需的参数时,我应该怎么做 var ParentClass = function(requiredParameter) { i

如果JavaScript中的“类”子类化是这样完成的:

var ParentClass = function() {
    // something
};


var ChildClass = function() {
    // something
};

ChildClass.prototype = new ParentClass();
。。。当父类具有必需的参数时,我应该怎么做

var ParentClass = function(requiredParameter) {
    if (typeof requiredParameter === 'undefined') {
        throw new TypeError("'requiredParameter' is required!");
    }
};


var ChildClass = function() {
    // something
};

ChildClass.prototype = new ParentClass();
// ^ Throws TypeError

谢谢。

改为如下子类:

function clone (obj) {
  if (!obj) return;
  clone.prototype = obj;
  return new clone();
}

var ParentClass = function() {
    // something
};


var ChildClass = function() {
    // something
};

ChildClass.prototype = clone(ParentClass.prototype);
ChildClass.prototype.constructor = ChildClass; // if you want

现在您不必担心它,因为您不必调用父构造函数对其进行子类化:)

一种更好的继承方法

var inherit = (function () {
  var F = function () {}; // cache function
  return function (C, P) { // Accepts Constructor and Parent
    F.prototype = P.prototype;
    // faster prototype chain lookup than direct instantiation
    C.prototype = new F(); 
    C._super = P.prototype;
    C.prototype.constructor = C; // for checking instanceof
  };
}());
这就是它的工作方式:

function Parent( a ) {
    this.a = a;
}

function Child( a, b ) {
    Parent.call( this, a ); // this is crucial
    this.b = b;
}

Child.prototype = Object.create( Parent.prototype );
Child.prototype.constructor = Child;
现场演示:(在控制台中分析实例)


你做这件事的方式

ChildClass.prototype = new ParentClass();
这是一个肮脏的黑客是坏的,应该避免。使用
Object.create
设置两个原型对象之间的继承关系

第二行

Child.prototype.constructor = Child;

有点随意。我们正在更正
构造函数
属性,因为我们必须覆盖
Child.prototype
以设置继承。如果你不关心
构造函数
属性,那就省去那一行。

我使用“init”函数,避免使用“非文字”原型(例如,我使用
。prototype={…}
。这当然是错误的,因为
新父类()
被急切地调用。你说的“非静态”是什么意思原型?你的意思是重新定义每个新实例的方法吗?通过“克隆”构造函数的原型而不是调用它们来扩展构造函数。通过将对象设置为伪函数的构造函数并使用
new
调用函数来克隆对象。请参阅我的(更新版)回答。@pst整洁、有趣。谢谢!Object.create不是很新吗?它是ES5的一部分,所以您需要自己在IE8和其他情况下实现它……或者在未更新的浏览器中实现。@user886931不,它已经“2年了。如果您关心较旧的浏览器,您需要ES5垫片……我想我们对“相当新”有不同的看法。)@用户886931不管怎样这都无关紧要。有ES5垫片,因此您可以安全地使用
对象。创建
。案例结束。
clone
功能已中断。您忘记了行
var clone=function(){}
…正如我们在下面(我的回答中)所确定的,有一个
对象。创建
,它完成了任务。因此,不需要自定义的
克隆
函数。它工作正常,如果您需要,我将设置一个JSFIDLE。正如我们在下面所确定的,Object.create是最近才出现的,这是一个完全合法的替代方案,基本上是长期以来一直采用的方式。这种方式比较草率。每次您想要继承时,都将被迫编写
.prototype=clone(parent.prototype)
以及构造函数。我的方法还有一个优点,就是通过
uber
Šime:。。。Trevor,我同意将所有这些封装到某种“继承”函数中更干净。我只是想让事情尽可能接近原始示例。好吧,把F放在inherit之外是安全的,这样你就可以重用同一个函数,而不是为每次要inherit的调用创建一个新函数。它可能会更有效一点。为什么这个函数在parens中?它不是一个立即调用的函数表达式…这就是为什么每次使用它时我都返回一个函数而不是创建F。没有参数吗?它接受
构造函数
父级
@利吉