1 <html>
2 <head>
3 <title>js的原型对象和constructor研究</title>
4 <head/>
5 <body>
6
7 <script >
8 /**
9 *javascript原型对象是一种特殊的实例,它提供一种在所有的实例中共享状态的机制
10 *当访问对象成员时,先在当前对象中查找,,如果没有找到,则到该对象的原型对象中进行查找,,,一直到Object
11 */
12
13 //为Object的原型对象添加属性
14 Object.prototype.sex = "male";
15
16 function Base(){
17 this.name = "eric";
18 }
19
20 Base.prototype = {
21 constructor:Base, //把原型对象的constructor指向Base
22 name:"prototype..",
23 greet: function(){ alert("hello...i'm " + this.name + "and i m " + this.sex)} //此处的sex继承自Object
24 }
25
26 var a = Base.prototype;
27 a.greet(); //prototype
28 var obj = new a.constructor; //用原型对象的构造方法创建对象
29 alert(obj.name); //eric
30 var b = new Base();
31 b.greet(); // eric
32
33 </script>
34 </body>
35 </html>