覆盖原型

//囚犯示例 
//1.定义原型对象 
var proto = { 
 sentence : 4, //监禁年限 
 probation: 2 //缓刑年限 
}; 
//2.定义原型对象的构造函数 
var Prisoner = function(name, id) { 
 this.name = name; 
 this.id = id; 
}; 
//3.将构造函数关联到原型 
Prisoner.prototype = proto; 
//4.实例化对象——采用工厂函数实例化对象 
var makePrisoner = function(name, id) { 
 //采用工厂函数实力化对象prisoner 
 var prisoner = Object.create( proto ); 
 prisoner.name = name; 
 prisoner.id = id; 
 return prisoner; 
}; 
 
var firstPrisoner = makePrisoner( 'Joe', '12A' ); 
 
//firstPrisoner.sentence在firstPrisoner对象找不到sentence属性, 
//所以查找对象的原型并找到了Both of these output 4 
console.log( firstPrisoner.sentence ); 
console.log( firstPrisoner.__proto__.sentence ); 
//把对象的sentence属性设置为10 
firstPrisoner.sentence = 10; 
//outputs 10 
//确定对象上的属性值已设置为10 
console.log( firstPrisoner.sentence ); 
//但是对象的原型并没有变化,值仍然为4 
console.log( firstPrisoner.__proto__.sentence ); 
//为了使获取到的属性回到原型的值,将属性从对象上删除 
delete firstPrisoner.sentence; 
//接下来,JavaScript引擎在对象上不能再找到该属性, 
//必须回头去查找原型链,并在原型对象上找到该属性 
// Both of these output 4 
console.log( firstPrisoner.sentence ); 
console.log( firstPrisoner.__proto__.sentence );

ubuntu 终端node输出

xxh@xxh-E440:~/workspace$ node t6 
4 
4 
10 
4 
4 
4 

那么如果改变了原型对象的属性值,会发生什么呢?我知道你在思考。

以上这篇浅谈JavaScript 覆盖原型以及更改原型就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

标签:
javascript,覆盖

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
评论“浅谈JavaScript 覆盖原型以及更改原型”
暂无“浅谈JavaScript 覆盖原型以及更改原型”评论...

P70系列延期,华为新旗舰将在下月发布

3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。

而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?

根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。