Questions tagged «prototype-programming»





3
基于原型与基于类的继承
在JavaScript中,每个对象同时是一个实例和一个类。要进行继承,可以将任何对象实例用作原型。 在Python,C ++等中。有类和实例作为单独的概念。为了进行继承,您必须使用基类创建一个新类,然后该新类可用于生成派生实例。 为什么JavaScript朝这个方向发展(基于原型的面向对象)?与传统的基于类的OO相比,基于原型的OO有哪些优点和缺点?

15
对数组中的属性值求和的更好方法
我有这样的事情: $scope.traveler = [ { description: 'Senior', Amount: 50}, { description: 'Senior', Amount: 50}, { description: 'Adult', Amount: 75}, { description: 'Child', Amount: 35}, { description: 'Infant', Amount: 25 }, ]; 现在要拥有这个数组的总数量,我正在做这样的事情: $scope.totalAmount = function(){ var total = 0; for (var i = 0; i < $scope.traveler.length; i++) { total …

8
__proto__与Constructor.prototype有何不同?
function Gadget(name, color) { this.name = name; this.color = color; } Gadget.prototype.rating = 3 var newtoy = new Gadget("webcam", "black") newtoy.constructor.prototype.constructor.prototype.constructor.prototype 它总是返回等级= 3的对象。 但是,如果我执行以下操作: newtoy.__proto__.__proto__.__proto__ 链条最终返回null。 另外,在Internet Explorer中,如果没有__proto__属性,如何检查null ?

3
Javascript继承:调用超级构造函数还是使用原型链?
最近,我读到有关MDC中JavaScript调用用法的信息 https://developer.mozilla.org/zh-CN/JavaScript/Reference/Global_Objects/Function/call 下面显示的示例的一个链接,我还是不明白。 他们为什么在这里使用继承 Prod_dept.prototype = new Product(); 这有必要吗?因为在这里有对超级构造函数的调用 Prod_dept() 反正像这样 Product.call 这只是常见的行为吗?什么时候使用调用超级构造函数或使用原型链更好? function Product(name, value){ this.name = name; if(value >= 1000) this.value = 999; else this.value = value; } function Prod_dept(name, value, dept){ this.dept = dept; Product.call(this, name, value); } Prod_dept.prototype = new Product(); // since 5 is less …
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.