You can:
Use prototypal inheritance to clone an existing object
Copy all of the properties of another object
function objectPlus(o, stuff) {
var n;
function F() {}
F.prototype = o;
n = new F();
n.uber = o;
for (var i in stuff) {
n[i] = stuff[i];
}
return n;
}
Start with the base shape object:
var shape = {
name: 'shape',
toString: function() {return this.name;}
}
Create a 2D object by inheriting shape and adding more properties. The additional properties are simply created in an anonymous object literal.
var twoDee = objectPlus(shape, {
name: '2D shape',
toString: function(){return this.uber.toString() + ', ' + this.name}
});
Now let's create a triangle object that inherits from 2D and adds some
more properties.
var triangle = objectPlus(twoDee, {
name: 'Triangle',
getArea: function(){return this.side * this.height / 2;},
side: 0,
height: 0
});
Testing how it all works by creating a concrete triangle my with defined side
and height:
var my = objectPlus(triangle, {side: 4, height: 4});
my.getArea()//8
my.toString()//shape,2d shape triangle,triangle
分享到:
相关推荐
and make them your own Understand objects in Google Chrome developer tools and how to use Them Use a mix of prototypal inheritance and copying properties in your workflow Apply reactive programming ...
and make them your own Understand objects in Google Chrome developer tools and how to use Them Use a mix of prototypal inheritance and copying properties in your workflow Apply reactive programming ...
means copying the objects and changing their properties. This book first covers fundamental mathematics for Big-O analysis and then lays out the basic JavaScript foundations, such as primitive objects...
inheritance pattern means copying the objects and changing their properties. This book first covers fundamental mathematics for Big-O analysis and then lays out the basic JavaScript foundations, such ...
JavaScript 数据结构与算法 2019年出版的新书 this book aims to teach data structure and algorithm ... However, the prototypal inheritance pattern means copying the objects and changing their properties.
This brief book explains the advantages of the object model, inheritance, both classical and prototypical, and shows how these concepts can be implemented in JavaScript. It also shows how object ...
This brief book explains the advantages of the object model, inheritance, both classical and prototypical, and shows how these concepts can be implemented in JavaScript. It also shows how object ...
If you want to write ... If you’re familiar with concepts such as closures and prototypal inheritance, you’ll be able to determine why some patterns may be more suitable for your projects than others.
If you’re already well-versed in advanced JavaScript concepts such as closures, prototypal inheritance, the dynamic nature of this, and feature detection, you may want to skim this part for a ...
Prototypal Inheritance, Prototype Cloning and the Flyweight Pattern, The Module Pattern, Unit Testing, Coming soon:, Architecting for Scale, Collaboration, Build, Continuous Integration, Deployment, ...
JavaScript 原型继承练习。
测验:复习JavaScript原型继承 ??? 复习JavaScript原型继承 ?:JavaScript的本机面向对象模型是什么? (X)原型()基于类()ES2016标准()原型 ...:由工厂函数创建的对象与构造函数创建的对象之间的主要区别是:...
angularjs 原型继承图AngularJS 范围原型继承 GraphViz 图。 这些图表用于 StackOverflow 上的答案。 在主要/长答案中使用了没有 StackOverflow ID 的那些: : 其他答案: : 要从点文件创建 png: dot some_file.dot...
原型继承测试需要js 原型继承+RequireJS的例子
I began working on MooTools in 2005, and, after a year of development, I released the very first version to the public. MooTools slowly gained popularity, ascending to its current position as one of ...
- Prototypal Inheritance - Immutability - Pure Functions - Higher Order Functions - Recursion - Currying - Partial Application - Lambdas 我将尽力解决这些问题。 尽管此处的大多数示例都将使用javascript...
Inheritance Section 5.1. Pseudoclassical Section 5.2. Object Specifiers Section 5.3. Prototypal Section 5.4. Functional Section 5.5. Parts Chapter 6. Arrays Section 6.1. Array Literals ...
研究Objects , Prototypal Inheritance和OOP ,以及OLOO :链接到其他对象的对象 可视化JavaScript:原型继承 Marcus Phillips:关于JavaScript类的秘密和谎言(低质量的视频,但绝对值得一看) 确保了解回调, ...
prototypal-oo-js-object-oriented-constructor-functions-lab-online-web-ft-110419-源码.rar