社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
function myfile(name,work){
var myself = new Object(); //创建一个新的对象
myself.name = name;
myself.work = work;
myself.sayWork = function(){
alert(this.work);
};
return myself; //返回这个新创建的对象
}
var myFile1 = myfile("zhang","IT");
myFile1.sayWork();
function Myfile(name,work){
this.name = name;
this.work = work;
this.sayWork = function(){
alert(this.work);
};
};
var myFile1 = new Myfile("zhang","IT");
myFile1.sayWork(); //IT
alert(Myfile.prototype.isPrototypeOf(myFile1)); //true
//我们用isPrototypeOf属性来检查myfile的原型对象与实例化对象myFile1之间的关系。
function Myfile(){
Myfile.prototype.name = "zhang";
Myfile.prototype.work = "IT";
Myfile.prototype.age = "100";
Myfile.prototype.sex = "nan";
Myfile.prototype.myself = function(){
alert(this.name + this.work + this.age + this.sex);
};
};
var myFile1 = new Myfile();
var myFile2 = new Myfile();
alert(myFile1.constructor == myFile2.constructor);
if((myFile1.name == myFile2.name) && (myFile1.work == myFile2.work) && (myFile1.age == myFile2.age) && (myFile1.sex == myFile2.sex)){
alert("这两个实例化对象所引用了同一个原型对象.");
}else{
alert("好吧,看来是我理解出错了!!!!");
}
function myfile(name,work,friend){
this.name = name;
this.work = work;
this.friend = friend;
};
myfile.prototype.showMySelf = function(){
alert(this.name + this.work + this.friend);
};
var myself = new myfile("zhang","IT",["hu","li","he"]);
var myself2 = new myfile("hu","teacher",["he","yi","li"]);
myself.showMySelf();
myself2.showMySelf();
function myfile(name, work) {
this.name = name;
this.work = work;
if (typeof this.myInfo != 'function') {
// 这段代码只执行了一次
alert("这是正确的");
myfile.prototype.myInfo = function() {
alert(this.name + this.work);
}
}
}
var myself = new myfile('zhang', 'IT'),myself2 = new myfile('hu', 'teacher');
myself2.myInfo();
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!