博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
TypeScript--es5中的类,继承,静态方法
阅读量:7083 次
发布时间:2019-06-28

本文共 3464 字,大约阅读时间需要 11 分钟。

最简单的类

function Person() {            this.name = 'lisi';            this.age = 20;        }        var p = new Person();        alert(p.name);

clipboard.png

构造函数和原型链里增加方法

function Person() {            this.name = 'lisi';            this.age = 20;            this.run = function() {                alert(this.name + '在运动');            }        }        Person.prototype.sex = '男';        Person.prototype.work = function() {            alert(this.name + '在工作');        }        var p = new Person();        alert(p.name);        p.run();        p.work()

clipboard.png

clipboard.png

clipboard.png

类里的静态方法

function Person() {            this.name = 'lisi';            this.age = 20;            this.run = function() {                alert(this.name + '在运动');            }        }        Person.getInfo = function() {            alert('我是静态方法')        }        Person.prototype.sex = '男';        Person.prototype.work = function() {            alert(this.name + '在工作');        }        var p = new Person();        // p.work();        Person.getInfo()

clipboard.png

es5里面的继承,对象冒充实现继承

只有一个弹框,说明没有继承到prototype上的方法

function Person() {            this.name = 'lisi';            this.age = 20;            this.run = function() {                alert(this.name + '在运动');            }        }        Person.prototype.sex = '男';        Person.prototype.work = function() {                alert(this.name + '在工作');            }            //Web类继承Person类        function Web() {            Person.call(this);        }        var w = new Web();        w.run();        w.work();

clipboard.png

es5里面的继承,原型链实现继承

function Person() {            this.name = 'lisi';            this.age = 20;            this.run = function() {                alert(this.name + '在运动');            }        }        Person.prototype.sex = '男';        Person.prototype.work = function() {                alert(this.name + '在工作');            }            //Web类继承Person类        function Web() {        }        Web.prototype = new Person();        var w = new Web();        w.run();        w.work();

clipboard.png

clipboard.png

原型链实现继承的问题

function Person(name, age) {            this.name = name;            this.age = age;            this.run = function() {                alert(this.name + '在运动');            }        }        Person.prototype.sex = '男';        Person.prototype.work = function() {            alert(this.name + '在工作');        }        function Web(name, age) {        }        Web.prototype = new Person();        var w = new Web('lisi', 20)        w.run()        w.work()

clipboard.png

clipboard.png

原型链+对象冒充的组合继承模式

function Person(name, age) {            this.name = name;            this.age = age;            this.run = function() {                alert(this.name + '在运动');            }        }        Person.prototype.sex = '男';        Person.prototype.work = function() {            alert(this.name + '在工作');        }        function Web(name, age) {            Person.call(this, name, age);        }        Web.prototype = new Person();        var w = new Web('lisi', 20)        w.run()        w.work()

clipboard.png

clipboard.png

原型链+对象冒充的另一种方式

function Person(name, age) {            this.name = name;            this.age = age;            this.run = function() {                alert(this.name + '在运动');            }        }        Person.prototype.sex = '男';        Person.prototype.work = function() {            alert(this.name + '在工作');        }        function Web(name, age) {            Person.call(this, name, age);        }        Web.prototype = Person.prototype;        var w = new Web('lisi', 20)        w.run()        w.work()

clipboard.png

clipboard.png

转载地址:http://zymml.baihongyu.com/

你可能感兴趣的文章
程序员必知8大排序3大查找(二)
查看>>
iphone crash 日志查看
查看>>
编译apache报错 No recognized SSL/TLS toolkit detected
查看>>
Python进阶03 模块
查看>>
mac基本命令
查看>>
IOC疑惑
查看>>
爱上一匹野马
查看>>
ADS的使用
查看>>
STL的string和wstring
查看>>
[ucgui] 仪表盘函数
查看>>
windows下的Nginx+Squid+Tomcat+Memcached集群
查看>>
Javascript玩转继承(一)
查看>>
[问题2014S15] 解答
查看>>
为人处世
查看>>
ios开发中用过的一些外部库总结 cocoapods list
查看>>
wcf系列5天速成——第一天 binding的使用(1)
查看>>
如何解决谷歌Chrome浏览器空白页的问题
查看>>
XSS检测工具 X5S/fiddler
查看>>
Linux守护进程的编程实现
查看>>
Linux查看端口信息命令
查看>>