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 ...
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 ...
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.
Prototypal Inheritance, Prototype Cloning and the Flyweight Pattern, The Module Pattern, Unit Testing, Coming soon:, Architecting for Scale, Collaboration, Build, Continuous Integration, Deployment, ...
JavaScript是一种广泛应用于Web开发的动态、弱类型、基于原型的脚本语言。在JavaScript中,对象的继承机制...在"prototypal-inheritance:JavaScript 原型继承练习"项目中,你可以通过实践进一步加深对这些概念的理解。
结合以上知识点,"prototypal-inheritance-test-require-js"项目可能包含若干个JavaScript文件,每个文件定义了一个或多个模块,它们之间通过原型继承建立对象的层次结构,同时使用RequireJS来管理这些模块的加载和...
测验:复习JavaScript原型继承 ??? 复习JavaScript原型继承 ?:JavaScript的本机面向对象模型是什么? (X)原型()基于类()ES2016标准()原型 ...:由工厂函数创建的对象与构造函数创建的对象之间的主要区别是:...
angularjs 原型继承图AngularJS 范围原型继承 GraphViz 图。 这些图表用于 StackOverflow 上的答案。 在主要/长答案中使用了没有 StackOverflow ID 的那些: : 其他答案: : 要从点文件创建 png: dot some_file.dot...
- 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类的秘密和谎言(低质量的视频,但绝对值得一看) 确保了解回调, ...
programming和prototypal inheritance (没有类的对象) JavaScript首先出现在 Navigator 2.0 浏览器中,并出现在 Netscape 的所有后续浏览器以及 Microsoft 从 Internet Explorer 3.0 开始的所有浏览器中 ...