gpt4 book ai didi

javascript - JavaScript 基于原型(prototype)的继承的好例子

转载 作者:IT王子 更新时间:2023-10-29 02:44:57 26 4
gpt4 key购买 nike

我使用 OOP 语言编程已有 10 多年,但我现在正在学习 JavaScript,这是我第一次遇到基于原型(prototype)的继承。我倾向于通过学习好的代码来学得最快。正确使用原型(prototype)继承的 JavaScript 应用程序(或库)的编写良好的示例是什么?您能否(简要地)描述如何/在何处使用原型(prototype)继承,以便我知道从哪里开始阅读?

最佳答案

如前所述,道格拉斯·克罗克福德 (Douglas Crockford) 的电影很好地解释了原因和方法。但是将它放在几行 JavaScript 中:

// Declaring our Animal object
var Animal = function () {

this.name = 'unknown';

this.getName = function () {
return this.name;
}

return this;
};

// Declaring our Dog object
var Dog = function () {

// A private variable here
var private = 42;

// overriding the name
this.name = "Bello";

// Implementing ".bark()"
this.bark = function () {
return 'MEOW';
}

return this;
};


// Dog extends animal
Dog.prototype = new Animal();

// -- Done declaring --

// Creating an instance of Dog.
var dog = new Dog();

// Proving our case
console.log(
"Is dog an instance of Dog? ", dog instanceof Dog, "\n",
"Is dog an instance of Animal? ", dog instanceof Animal, "\n",
dog.bark() +"\n", // Should be: "MEOW"
dog.getName() +"\n", // Should be: "Bello"
dog.private +"\n" // Should be: 'undefined'
);

然而,这种方法的问题在于,它会在您每次创建对象时重新创建对象。另一种方法是在原型(prototype)堆栈上声明您的对象,如下所示:

// Defining test one, prototypal
var testOne = function () {};
testOne.prototype = (function () {
var me = {}, privateVariable = 42;
me.someMethod = function () {
return privateVariable;
};

me.publicVariable = "foo bar";
me.anotherMethod = function () {
return this.publicVariable;
};

return me;

}());


// Defining test two, function
var testTwo = ​function() {
var me = {}, privateVariable = 42;
me.someMethod = function () {
return privateVariable;
};

me.publicVariable = "foo bar";
me.anotherMethod = function () {
return this.publicVariable;
};

return me;
};


// Proving that both techniques are functionally identical
var resultTestOne = new testOne(),
resultTestTwo = new testTwo();

console.log(
resultTestOne.someMethod(), // Should print 42
resultTestOne.publicVariable // Should print "foo bar"
);

console.log(
resultTestTwo.someMethod(), // Should print 42
resultTestTwo.publicVariable // Should print "foo bar"
);



// Performance benchmark start
var stop, start, loopCount = 1000000;

// Running testOne
start = (new Date()).getTime();
for (var i = loopCount; i>0; i--) {
new testOne();
}
stop = (new Date()).getTime();

console.log('Test one took: '+ Math.round(((stop/1000) - (start/1000))*1000) +' milliseconds');



// Running testTwo
start = (new Date()).getTime();
for (var i = loopCount; i>0; i--) {
new testTwo();
}
stop = (new Date()).getTime();

console.log('Test two took: '+ Math.round(((stop/1000) - (start/1000))*1000) +' milliseconds');

在内省(introspection)方面有一个轻微的缺点。转储 testOne,将导致有用信息减少。此外,“testOne”中的私有(private)属性“privateVariable”在所有实例中都是共享的,shesek 在回复中也提到了这一点。

关于javascript - JavaScript 基于原型(prototype)的继承的好例子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2064731/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com