JavaScript Prototype Explained With Examples

Prototypes

JavaScript is a prototype-based language, therefore understanding the prototype object is one of the most important concepts which JavaScript practitioners need to know. This article will give you a short overview of the Prototype obejct through various examples. 

Prototype object

For the sake of clarity, let's examine the following example:

function Point2D(x, y) {
    this.x = x;
    this.y = y;
}

As  Point2D function is declared, a default property named prototype will be created for it(note that, in JavaScript, a function is also an object).The prototype property is an object which contains a constructor property and its value is Point2D function: Point2D.prototype.constructor = Point2D. And when you call Point2D with new keyword, newly created objects will inherit all properties from Point2D.prototype. To check that, you can add a method named move into Point2D.prototype as follows:

Point2D.prototype.move = function(dx, dy) {
    this.x += dx;
    this.y += dy;
}

var p1 = new Point2D(1, 2);
p1.move(3, 4);
console.log(p1.x);
console.log(p1.y);

The Point2D.prototype is called prototype object or prototype of p1 object and for any other object created with new Point2D(...) syntax. You can add more properties to Point2D.prototype obejct as you like. The common pattern is declare methods to Point2D.prototype and other properties will be declared in constructor function.

Buil-in Objects in Javascript are constructed in a similar manner. For example:

Prototype of objects created with new Object() or {} syntax is Object.prototype.

Prototype of arrays created with new Array() or {} syntax is Array.prototype.

And so on with other built-in objects such ad Date and RegExp.

Object.prototype is inherited by all objects and it has no prototype(its prototype is null).

Prototype chain

The prototype chain mechanism is simple: When you access a property p on object obj,the Javascript engine will search this property inside obj object. If the engine fails to search, it continues searching in the prototype of obj object and so on until reaching Object.prototype. If after the search has finished, and nothing has been found the result will be undefined. For example:

var obj1 = {
    a: 1,
    b: 2
};

var obj2 = Object.create(obj1);
obj2.a = 2;

console.log(obj2.a); // 2
console.log(obj2.b); // 2
console.log(obj2.c); // undefined

In above snippet, the statement var obj2 = Object.create(obj1) will create obj2 object with prototype obj1 object. In other words, obj1 becomes the prototype of obj2 instead of Object.prototype by default. As you can see, b is not a property of obj2, you can still access it via the prototype chain. For c property, however, you get undefined value because it can't be found in obj1 and Object.prototype.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值