var Person = function(){ var Person = function(name) { this.name = name; console.log("我是父类布局函数"); } Person.prototype.name = ""; return Person;}();var Student = function(){ var Student = function(name){ Person.prototype.constructor.call(this,name); console.log("我是子类布局函数"); } Student.prototype ={ get age(){ console.log("我是getter"); return this._age; }, set age(val){ console.log("我是setter",val); this._age = val; } } Student.prototype.shout=function(){ console.log(this.name); } return Student;}();var student = new Student("zsf");student.age = 99;console.log(student.age);student.shout();
注:js中没有接口的观点,但是在ts(typescrpt)中是有接口的观点,ts是js的超级,更新近于我们常见的高等措辞的oop的写法,类如java,c#,php等!
我们以上用两种写法写了js实现类和继续,以及getter和setter写法,class的写法,大家如果看过之前的php编程就比较好理解了。function闭包的写法,紧张是依据于js中的prototype工具,这个是js实现oop的核心,你即利用class的写法,其内部也是基于prototype的写法来写的!
在大家可以把prototype理解成extend的意思,某个function的prototype=obj,表示这个function里包含了obj里的属性和方法。prototype属于原型链继续。
