靜態(tài)成員
定義在構(gòu)造函數(shù)上面的成員(屬性和方法)
實(shí)例成員
定義在實(shí)例對(duì)象上面的成員(屬性和方法)
建議
① 把工具類的方法寫成靜態(tài)方法
② 把和對(duì)象相關(guān)的方法寫成實(shí)例方法(成員)
代碼示例
function Person() {
this.name = "張三"; //實(shí)例屬性
this.showName = function () {
console.log(this.name);
};
}
//為Person構(gòu)造函數(shù)添加靜態(tài)成員
Person.des = "描述信息";
Person.add = function (msg) {
console.log("添加信息" + msg);
};
Person.add("這是一個(gè)+操作");
var p1 = new Person();
p1.showName();