js对象的创建和继承
来源:互联网 发布:linux查看普通用户 编辑:程序博客网 时间:2024/05/24 01:18
js对象的创建和继承
1、对象的创建
创建对象的设计模式有很多种,这里采用动态原型模式,是最优的方案。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"><title>Document</title></head><body><script type="text/javascript">function Person (name,age,job) {// 属性this.name = name;this.age = age;this.job = job;this.arr = ["hehe","hh"];//方法if(typeof this.sayName != "function"){Person.prototype.sayName = function(){alert(this.name);};}}var friend = new Person("Nicholas",29,"Soft Engineer");friend.arr.push("ee");alert(friend.arr);var friend1 = new Person("mike",20,"ss");alert(friend1.arr);</script></body></html>
2、对象的继承
关于对象继承的设计模式有很多种,这里采用寄生组合式继承,是最优的方案。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"><title>Document</title></head><body><script type="text/javascript">function object(o){function F(){}F.prototype = o;return new F();}function inheritPrototype (subType,superType) {var prototype = object(superType.prototype);prototype.constructor = subType;subType.prototype = prototype;}function SuperType(name){this.name = name;this.colors = ["red","blue","green"];}SuperType.prototype.sayName = function(){alert(this.name);};function SubType(name,age){SuperType.call(this,name);this.age = age;}inheritPrototype(SubType,SuperType);SubType.prototype.sayAge = function(){alert(this.age);};var subType1 = new SubType("mary",20);var superType1 = new SuperType("aa",22);subType1.colors.push("element");superType1.colors.push("hhe");subType1.sayAge();subType1.sayName();console.log(subType1 instanceof SubType);console.log(subType1 instanceof SuperType);console.log(subType1.colors);console.log(superType1.colors);console.log(subType1.constructor == superType1.constructor);</script></body></html>
0 0
- JS对象的创建和继承
- js对象的创建和继承
- js创建对象和继承
- JS如何创建对象和继承对象
- js中创建对象和继承的几种方式
- 理解js的对象,对象创建,继承
- js创建对象继承
- js 对象深复制,创建对象和继承
- js 对象深复制,创建对象和继承
- 创建对象和继承
- 基于原型的对象创建和继承
- JavaScript对象的创建和继承实现
- javascript对象的创建和继承实现
- js(十六)——原型创建对象和继承
- js创建对象的构造函数模式+原型模式和组合继承\Hybrid Pattern & combination inheritance
- js对象的继承
- js对象的继承
- js面向对象和继承
- High Performance Mysql 读书笔记——创建高性能索引
- Codeforces Round #326 (Div. 1) C. Duff in the Army
- nginx的缓存设置 expires缓存提升网站负载
- 如何利用MVC+EF实现前台传值
- Spring.NET学习笔记2——环境搭建(基础篇) Level 200
- js对象的创建和继承
- ios开发之个人笔记(通过plist文件展示单组数据LOL)
- 组装服务器注意事项
- cakePHP 入门与应用
- 《非同儿戏的数据》
- SercureCRT&CuteFTP实现windows与虚拟机linux互联
- 事件分发机制的原理总结
- 即时通回归 挟Flickr抢通讯市场
- 基于jse的金蝶K3ERP-BOM物料编码位置号比对工具(输出excel)