/***我是切割线 的開始***/
//利用prototype属性能够加入公有属性和方法
function myConstructor2(){
this.a='大灰狼';
}; //声明构造函数,能够使用对象字面量语法来向prototype属性中加入全部公有成员
myConstructor2.prototype={
propertyA: 'sha' ,
propertyB: 'feng' ,
methodA:function(){
alert(this.propertyA);
},
methodB:function(){}
}
var myconstrustor=new myConstructor2(); //声明对象
//alert(myconstrustor.methodA());
var myconstrustor1=new myConstructor2(); //声明对象
var myconstrustor2=new myConstructor2(); //声明对象
alert("原型类測试结果="+(myconstrustor1.propertyB === myconstrustor2.propertyB));
alert("构造函数測试结果="+(myconstrustor1.a === myConstructor2.a));
/***我是切割线 的结束***/
function member(name, sex) {
this.name = name;
this.sex = sex;
this.display = display;
}
function display() {
var str = this.name + "是" + this.sex;
document.write("<LI>" + str);
}
var papa = new member("杨宏文", "男生");
var mama = new member("黄雅玲", "女生");
var doggy = new member("奇 奇", "宠物狗");
papa.display();
mama.display();
doggy.display();
/*
*/
总结
4.能够总结出几条规律:
1:当实例本身定义了属性。使用本身属性(能够使用hasOwnProperty进行推断);2:假设实例本身未定义。就到构造函数中去找;
3:假设构造函数也没有,就到构造函数的prototype的实例对象上去找。
4:假设prototype的实例对象没有,就到其构造函数去找;
5:如此反复,一直到跟对象object中,没找到就报undefined错误。