JavaScript 对象从一个原形对象(prototype object) 继承属性。所有对象都有原型;原型的所有属性看上去就像使用它作为原型的那些对象的属性一样。简单的说就是:所有对象都从他的原型继承属性。 
(each object inherits properties from its prototype).
对象的 prototype 通过它的 constructor function 来定义。JavaScript 里所有的 function 都有一个 prototype 属性。这个属性开始是空的,接下来你给他添加的任何属性都会被 constructor 创建的对象所拥有。
prototype 对象和 constructor 相关联。这意味着 prototype 可以作为放置方法以及其他常量的理想场所。原型中的属性不会被复制到新创建的对象中去,他们的属性看上去就像对象的属性一样。这意味着,使用原型能够大幅度的减少多个同类对象占用的内存。
每一个 class 都只有一个 prototype object, 附带一套属性。但我们在运行时可能会创建多个类的实例。那么如果发生对原型的属性的读写会发生什么情况?
当你读一个属性的时候,JavaScript 首先尝试去查找对象本身是否有这个属性,如果没有,接着去查找原型里面是否有。有的话就返回结果。
而当你写原型的属性的时候,因为多个对象共享原型,显然是不能直接在原型上进行写操作的。这个时候实际上 JavaScript 会在对象上创建一个同名的属性,然后把值写到里面。当你下次读这个属性的时候,JavaScript 一下子就在对象的属性里查找到了,那么就不需要去原型里查找了。这个时候,我们说“对象的属性掩盖或隐藏了原型的属性”。(shadows or hides) 。
从上面讨论看出,其实我们在设计类的时候,只要掌握一个原则:在原型里仅定义一些方法(方法一般是不会变的),常数,常量等。做到这一点就不容易混淆了。
例子:
// Define a constructor method for our class. 
// Use it to initialize properties that will be different for 
// each individual Circle object. 
functionCircle(x, y, r) 
{ 
    this.x = x;  // The X-coordinate of the center of the circle 
 
this.y = y; // The Y-coordinate of the center of the circle
 
this.r = r; // The radius of the circle
} 
 
 
// Create and discard an initial Circle object. 
// This forces the prototype object to be created in JavaScript 1.1. 
new Circle(0,0,0); 
 
 
// Define a constant: a property that will be shared by 
// all circle objects. Actually, we could just use Math.PI, 
// but we do it this way for the sake of instruction. 
Circle.prototype.pi = 
3.14159;
 
 
// Define a method to compute the circumference of the circle. 
// First declare a function, then assign it to a prototype property. 
// Note the use of the constant defined above. 
function Circle_circumference(  ) { return 
2
*
this.pi *
this.r; }
Circle.prototype.circumference =Circle_circumference; 
 
 
// Define another method. This time we use a function literal to define 
// the function and assign it to a prototype property all in one step. 
Circle.prototype.area = 
function( ) { return
this.pi *
this.r *
this.r; }
 
 
// The Circle class is defined. 
// Now we can create an instance and invoke its methods. 
var c = 
new Circle(0.0, 0.0, 1.0);
var a =c.area(  ); 
var p = c.circumference(  ); 
内置的类的 prototype.
不光是用户自定义的类可以有 prototype. 系统内置的类比如 String, Date 也都有的。而且你可以向他们添加新的方法,属性等。
下面这段代码就对所有的 String 对象添加了一个有用的函数:
// Returns true if the last character is c 
String.prototype.endsWith = 
function(c) {
    return (c == 
this.charAt(this.length-1))
} 
然后我们就可以类似这样的来调用了:
var message = 
"hello world";
message.endsWith('h')  // Returns false 
message.endsWith('d')  // Returns true
                                (each object inherits properties from its prototype).
对象的 prototype 通过它的 constructor function 来定义。JavaScript 里所有的 function 都有一个 prototype 属性。这个属性开始是空的,接下来你给他添加的任何属性都会被 constructor 创建的对象所拥有。
prototype 对象和 constructor 相关联。这意味着 prototype 可以作为放置方法以及其他常量的理想场所。原型中的属性不会被复制到新创建的对象中去,他们的属性看上去就像对象的属性一样。这意味着,使用原型能够大幅度的减少多个同类对象占用的内存。
每一个 class 都只有一个 prototype object, 附带一套属性。但我们在运行时可能会创建多个类的实例。那么如果发生对原型的属性的读写会发生什么情况?
当你读一个属性的时候,JavaScript 首先尝试去查找对象本身是否有这个属性,如果没有,接着去查找原型里面是否有。有的话就返回结果。
而当你写原型的属性的时候,因为多个对象共享原型,显然是不能直接在原型上进行写操作的。这个时候实际上 JavaScript 会在对象上创建一个同名的属性,然后把值写到里面。当你下次读这个属性的时候,JavaScript 一下子就在对象的属性里查找到了,那么就不需要去原型里查找了。这个时候,我们说“对象的属性掩盖或隐藏了原型的属性”。(shadows or hides) 。
从上面讨论看出,其实我们在设计类的时候,只要掌握一个原则:在原型里仅定义一些方法(方法一般是不会变的),常数,常量等。做到这一点就不容易混淆了。
例子:
// Define a constructor method for our class. 
// Use it to initialize properties that will be different for 
// each individual Circle object. 
functionCircle(x, y, r) 
{ 
    this.x = x;  // The X-coordinate of the center of the circle 
 this.y = y; // The Y-coordinate of the center of the circle
 this.r = r; // The radius of the circle
} 
 
 
// Create and discard an initial Circle object. 
// This forces the prototype object to be created in JavaScript 1.1. 
new Circle(0,0,0); 
 
 
// Define a constant: a property that will be shared by 
// all circle objects. Actually, we could just use Math.PI, 
// but we do it this way for the sake of instruction. 
Circle.prototype.pi = 3.14159;
 
 
// Define a method to compute the circumference of the circle. 
// First declare a function, then assign it to a prototype property. 
// Note the use of the constant defined above. 
function Circle_circumference(  ) { return 2
*
this.pi *
this.r; }
Circle.prototype.circumference =Circle_circumference; 
 
 
// Define another method. This time we use a function literal to define 
// the function and assign it to a prototype property all in one step. 
Circle.prototype.area = function( ) { return
this.pi *
this.r *
this.r; }
 
 
// The Circle class is defined. 
// Now we can create an instance and invoke its methods. 
var c = new Circle(0.0, 0.0, 1.0);
var a =c.area(  ); 
var p = c.circumference(  ); 内置的类的 prototype.
不光是用户自定义的类可以有 prototype. 系统内置的类比如 String, Date 也都有的。而且你可以向他们添加新的方法,属性等。
下面这段代码就对所有的 String 对象添加了一个有用的函数:
// Returns true if the last character is c 
String.prototype.endsWith = function(c) {
    return (c == this.charAt(this.length-1))
} 然后我们就可以类似这样的来调用了:
var message = "hello world";
message.endsWith('h')  // Returns false 
message.endsWith('d')  // Returns true
                                    标签:
                                        
                            Javascript,原型,继承
                                免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
                                如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
                            
                        白云城资源网 Copyright www.dyhadc.com
                        暂无“Javascript 原型和继承(Prototypes and Inheritance)”评论...
                                    稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
2025年11月04日
                                2025年11月04日
                    - 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
 - 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
 - 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
 - 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
 - 群星《2024好听新歌42》AI调整音效【WAV分轨】
 - 王思雨-《思念陪着鸿雁飞》WAV
 - 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
 - 李健《无时无刻》[WAV+CUE][590M]
 - 陈奕迅《酝酿》[WAV分轨][502M]
 - 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
 - 群星《吉他王(黑胶CD)》[WAV+CUE]
 - 齐秦《穿乐(穿越)》[WAV+CUE]
 - 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
 - 邝美云《邝美云精装歌集》[DSF][1.6G]
 - 吕方《爱一回伤一回》[WAV+CUE][454M]