Javascript使用构造函数创建“类class”
Javascript没有类的概念,只有构造函数。使用构造函数创建“类class”,实例变量和实例方法写在构造函数里面,静态变量和静态方法要写在构造函数外面。
(如果把静态变量和方法写在构造函数里面,只能先实例化再调用静态方法了)
function Person(config) {
// 实例变量
this.config = config;
// 实例方法
this.fun = function () {
};
// 原型方法,也是实例方法
Person.prototype.init = function () {
console.log('初始化');
};
}
// 静态变量
Person.count = 0;
// 静态方法
Person.Init = function () {
console.log("我是 Init");
// Person.Instance = new Person();
// Person.Instance.init();
}