Design Patterns(4)--Creational: Prototype Pattern-Nothing is too small in prototype

Design Patterns(4)--Creational: Prototype Pattern-Nothing is too small in prototype

·

1 min read

When creating an object in the prototype pattern, we will find an origin object as a prototype. And we clone the prototype to create a new object. They have the same prototype and share the data and methods. object.create() is the self-realization of prototype pattern in javascript. As lone as using prototype to realize object creation and prototypal inheritance in javascript, we use the prototype pattern.

We can learn about prototype and __proto__ in javascript to better understand the prototype pattern. Too many related posts on the Inernet, so I don't write more details about it.

ch

设计模式(四)——创建型:原型模式-prototype无小事

在原型模式下,当我们想要创建一个对象时,会先找到一个对象作为原型,然后通过克隆原型的方式来创建出一个与原型一样(共享一套数据/方法)的对象。在JavaScript里,object.create方法就是原型模式的天然实现——准确地说,只要我们还在借助prototype来实现对象的创建和原型的继承,那么我们就是在应用原型模式。

我们可以了解在JavaScript中的原型和原型链,去更好地了解原型模式。在网上有很多关于原型和原型链的文章,所以我不会再此赘述。