JavaScript对象之面向对象

在js中创建对象的两种方式

1.new一个Object
eg:

var flower = new Object();
flower.stuname = "呵呵";
flower.age = 20;
flower.address = '台湾';
flower.showInfo = function(){
alert(this.stuname+'\n'+this.age+'\n'+this.address);
};
flower.showInfo();

 

2,使用字面量赋值的方式创建对象
eg:

var flower ={
stuname :'张小娴',
stuage :20,
address:'台湾',
showInfo:function(){
alert(this.stuname+'\n'+this.stuage+'\n'+this.address);
}

};
flower.showInfo();
flower.stuname='啦啦';
alert(flower.stuname);

 

3.原型(prototype)

 

其实原型就是一种类型,类似于Java中的一种反射机制  Java中自定义一个Person类,他就是一个类型。

Class  p1 = Person.class.getClassLoder(); //类似与Person.prototype  

 原型就是一个对象,它可以像普通对象一样去存储=(属性+方法),而且默认它自己维护这一个constructor属性。这个Constructor指向了原型的构造函数,原型构造就是我们一直在讨论的约定俗成的伪类(函数名称首字母大写后的一个不像类型的方法)。

二、原型对象图

 

 

 

 

 ①所有函数的__proto__都是指向Function的prototype。

②构造函数new出来的对象__proto__指向构造函数的prototype。

③非构造函数实例化出的对象或者对象的prototype的__proto__指向Object的prototype。

④Object的prototype指向null。

 

posted on 2018-07-02 15:35  技术之路永无止境~  阅读(97)  评论(0编辑  收藏  举报

导航