JavaScript2种构造函数创建对象的模式以及继承的实现
2024-09-08 12:11:38
第一种模式:
function Person(){
}
Person.prototype.say=function(){
alert('hello');
}
var person=new Person();
person.say();//hello
依据第一种模式说一下继承的实现:
function Person(){
}
Person.prototype.say=function(){
alert('hello');
}
function Man(){}
Man.prototype=new Person()
var man=new Man();
man.say(); //hello
另外一种模式:
function Person(){
var _this={};//创建一个空的对象
_this.say=function(){alert('hello')};
return _this;
}
function person=new Person();
person.say();//hello
另外一种模式的继承:
function Person(){
var _this={};//创建一个空的对象
_this.say=function(){alert('hello')};
return _this;
}
function Man(){
var _this=new Person();
return _this;
}
var a=new Man();
a.say();//hello
本文作者:罗坚元
最新文章
- ASP.NET MVC——模型绑定
- maxscale
- lkx开发日志1-项目前的准备
- text-indent无效解决方案
- Apache索引目录浏览的学习笔记
- Ajax提交后台中文乱码问题
- [Google Translation API v2 for Java]
- POJ 2418 Hardwood Species
- OC中修饰符:宏define 常量:const extern
- [MySQL] 同步一张表、复制过滤设置
- Office2016 转换零售版为VOL版
- UESTC_秋实大哥与快餐店 2015 UESTC Training for Data Structures<;Problem C>;
- 201521123080《Java程序设计》第9周学习总结
- Query DSL(1)
- CentOS6.x机器安装Azure CLI2.0【1】
- pssac plot
- cocoapods卸载与安装
- Simple2D-26 Simple2D 最后的工作,开发结束
- istview android:cacheColorHint,android:listSelector属性作用(转)
- Spring中的注解配置-注入bean
热门文章
- iOS学习笔记01-APP相关
- NOJ——1274The battle of Red Cliff(并查集按秩合并)
- HDU——4565So Easy!(矩阵快速幂)
- 解决v-for产生的警告的办法
- spring boot-html和templates
- bzoj 2300 [HAOI2011]防线修建 set动态维护凸包
- 【HDOJ5974】A Simple Math Problem(构造,解方程)
- WebRTC VoiceEngine综合应用示例(一)——基本结构分析(转)
- 【Visual Studio】error C2220: 警告被视为错误 - 没有生成“object”文件 (转)
- http协议相关面试题