日期:2014-05-16 浏览次数:20418 次
var oPerson = new Object; oPerson.name = "yzl"; oPerson.sex = "female"; oPerson.sayInfo = function(){ alert("name : " + this.name + " ,sex :" + this.sex); };
function createPerson(){ var oPerson = new Object; oPerson.name = "yzl"; oPerson.sex = "female"; oPerson.sayInfo = function(){ alert(" name : " + this.name + " ,sex :" + this.sex); }; return oPerson; }; var p1 = createPerson(); p1.sayInfo(); p1.name = "gcm"; p1.sex = "female"; p1.sayInfo();
function createPerson(sName,sSex){ var oPerson = new Object; oPerson.name = sName; oPerson.sex = sSex; oPerson.sayInfo = function(){ alert("name : " + this.name + " ,sex :" + this.sex); }; return oPerson; }; var p1 = createPerson("yzl","male"); var p2 = createPerson("gcm","female"); p1.sayInfo(); p2.sayInfo();
function sayInfo(){ alert("name : " + this.name + " ,sex :" + this.sex); } function createPerson(sName,sSex){ var oPerson = new Object; oPerson.name = sName; oPerson.sex = sSex; oPerson.sayInfo = sayInfo; return oPerson; }; var p1 = createPerson("yzl","male"); var p2 = createPerson("gcm","female"); p1.sayInfo(); p2.sayInfo();
function sayInfo(){ alert("name : " + this.name + " ,sex :" + this.sex); } function Person(sName,sSex){ this.name = sName; this.sex = sSex; this.sayInfo = sayInfo; } var p1 = new Person("yzl","male"); p1.sayInfo();
function Person(){ } Person.prototype.name = "yzl"; Person.prototype.sex = "male"; Person.prototype.sayInfo = function(){ alert("name : " + this.name + " ,sex :" + this.sex); }; var p1 = new Person(); p1.sayInfo(); p1.name = "gcm"; p1.sex = "female"; p1.sayInfo();
alert(p1 instanceof Person); //true
function Person(){ } Person.prototype.name = "yzl"; Person.prototype.sex = "male"; Person.prototype.friends = new Array("ymj","gcm"); Person.prototype.sayInfo = function(){ alert("name : " + this.name + " ,sex :" + this.sex); }; var p1 = new Person(); var p2 = new Person(); p1.friends.push("hl"); alert(p2.friends); // ym