js 原生对象加属性
js
person=new Object();
person.firstname="John";
person.lastname="Doe";
person.age=50;
person.eyecolor="blue";
等于
person={firstname:"John",lastname:"Doe",age:50,eyecolor:"blue"};
-----------------------------
使用 prototype 属性就可以给对象的构造函数添加新的属性和新的方法:
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
}
Person.prototype.nationality = "English";
Person.prototype.name = function() {
return this.firstName + " " + this.lastName;
};