首页 » 网站建设 » phpmycrpt技巧_web开拓之js类的实现办法

phpmycrpt技巧_web开拓之js类的实现办法

访客 2024-11-10 0

扫一扫用手机浏览

文章目录 [+]

function实现js类

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();

phpmycrpt技巧_web开拓之js类的实现办法

注: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属于原型链继续。

phpmycrpt技巧_web开拓之js类的实现办法
(图片来自网络侵删)

相关文章

轻付出php技巧_PHP的支付宝APP支付

文|何掌柜第一步:创建运用并获取APPID要在您的运用中利用支付宝开放产品的接谈锋能,您须要先去蚂蚁金服开放平台(open.ali...

网站建设 2024-12-11 阅读0 评论0