function Person(){}; var person = new Person(); person.name = 'yy'; person.gender = 'girl'; console.log(person.name+','+person.gender);//yy,girl delete person.name;//刪除屬性 console.log(person.name);//undefined // 變式 function Person(){}; var person = new Person(); person['name'] = 'yy'; person['gender'] = 'girl'; console.log(person.name+','+person.gender);//yy,girl delete person.name;//刪除屬性 console.log(person.name);//undefined
var Person = function (){ this.name = 'yy'; this.gender = 'girl' }; var person = new Person(); console.log(person.name+','+person.gender);//yy,girl
var Person = function (){ }; Person.prototype.name = 'yy'; Person.prototype.gender = 'girl'; var person = new Person(); console.log(person.name+','+person.gender);//yy,girl console.log(person['name']+','+person['gender']);//yy,girl
var person = { }; person.name = 'yy'; person.gender = 'girl'; console.log(person.name+','+person.gender);//yy,girl console.log(person['name']+','+person['gender']);//yy,girl // 另一種寫法 var person = { name: 'yy', gender: 'girl' }; console.log(person.name+','+person.gender);//yy,girl console.log(person['name']+','+person['gender']);//yy,girl
var p1 = {}; var p2 = {name:'yy',gender:'girl'}; p1.extend = function(obj){ for(var k in obj){ this[k] = obj[k]; } } p1.extend(p2); console.log(p1.name+','+p1.gender);//yy,girl console.log(p1['name']+','+p1['gender']);//yy,girl