function Person(name, age) {
this.name = first;
this.age = age;
}
var father = new Person("wang", 18);
向对象中添加成员
向对象中添加属性,只对对象有效,对类无效
father.score=98;
向对象中添加方法
father.eat = function({return "father eat"});
向类中添加成员
所有javascript对象都从原型继承属性和方法.数组对象继承Array.prototype,Person对象继承自Person.prototype
向类中添加属性
Person.prototype.score = 98;
向类中添加方法
Person.prototype.eat = function({return "father eat"});
getter setter
getter
function Person(name, age) {
this.name = first;
this.age = age;
get wName(){return this.name};
}
var father = new Person("wang", 18);
father.wName
setter
function Person(name, age) {
this.name = first;
this.age = age;
set wname(newName){this.name = newName};
}
var father = new Person("wang", 18);
father.wname("wei")
访问对象属性的方式:
1.objectName.property // person.age
一般形式
2.objectName["property"] // person["age"]
python字典形式
3.objectName[expression] // x = "age"; person[x]
特殊形式