Javascript对象学习2 - 继承
2013-10-16 14:00
363 查看
<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title>Javascript的继承的简单学习</title> </head> <body> <script type="text/javascript"> (function(){ /** * Javascript的继承的简单学习 * * 1.对象冒充方式 */ /* //被继承对象 function User(name) { this.name = name; this.sayName = function() { alert(this.name); } } function Student(name,age) { this.temp = User; this.temp(name); //删除对User的引用,确保后续代码不能再次通过temp操作User,要不this.temp("aa");还会起作用 //delete this.temp; //this.temp("aa"); //新属性和新方法都必须在删除新方法的代码行后定义。否则,可能会覆盖父类相关的属性和方法 //this.name = "bb"; this.age = age; this.showAge = function() { alert(this.age); } } //调用 var stu1 = new Student("tom",23); stu1.sayName(); stu1.showAge(); */ /** * 1.1.继承:对象冒充方式(多重继承) */ /* //被继承对象1 function User(name) { this.name = name; this.sayName = function() { alert(this.name); } } //被继承对象2 function Like(item) { this.item = item; // this.name = ""; this.play = function() { alert(this.item); } } function Student(name,age,item) { this.user = User; this.user(name); delete this.user; this.like = Like; this.like(item); delete this.like; this.age = age; this.showAge = function() { alert(this.age); } } //调用 var stu1 = new Student("Rose",25,"basketball"); stu1.sayName(); stu1.showAge(); stu1.play(); //缺点:如果存在两个类User和Like具有同名的属性或者方法,Like具有更高的优先级 */ /** * 2.继承:使用call()方法 */ /* function show(age,item) { alert(this.name + " --- " + age + " --- " + item); } var obj = new Object(); obj.name = "tom"; //调用 show.call(obj,21,"football"); //第一个参数最好不要少 //相当于:obj.show = show(); → show(21,"football") */ /** * 2.1 继承:call()方法继承,多重继承 */ /* //被继承对象1 function User(name) { this.name = name; this.sayName = function() { alert(this.name); } } //被继承对象2 function Like(item) { this.item = item; // this.name = ""; this.play = function() { alert(this.item); } } function Student(name,age,item) { User.call(this,name); Like.call(this,item); this.age = age; this.showAge = function() { alert(this.age); } } //调用 var stu1 = new Student("Rose",25,"basketball"); stu1.sayName(); stu1.showAge(); stu1.play(); alert(stu1 instanceof User);//false alert(stu1 instanceof Like);//false alert(stu1 instanceof Student);//true alert(stu1.constructor); //function Sudent().... */ /** * 3.继承:apply()方法 */ /* //被继承对象1 function User(name) { this.name = name; this.sayName = function() { alert(this.name); } } //被继承对象2 function Like(item) { this.item = item; // this.name = ""; this.play = function() { alert(this.item); } } function Student(name,age,item) { User.apply(this,[name]);//变化之处 Like.call(this,[item]); this.age = age; this.showAge = function() { alert(this.age); } } //调用 var stu1 = new Student("Rose",25,"basketball"); stu1.sayName(); stu1.showAge(); stu1.play(); alert(stu1 instanceof User);//false alert(stu1 instanceof Like);//false alert(stu1 instanceof Student);//true alert(stu1.constructor); //function Sudent().... */ /** * 4.继承:原型连方式 */ //被继承对象 /* function User() {} User.prototype.name = ""; User.prototype.sayName = function() { alert(this.name); }; function Student() {} //①将User中所有通过prototype设置的属性或方法放到Student中,并覆盖了Student中所有的prototype设置。所以应该放在第一行 Student.prototype = new User(); Student.prototype.age = 0; Student.prototype.showAge = function() { alert(this.age); } //Student.prototype = new User(); //一定要放在第一行哦 var stu = new Student(); stu.name = "tom"; stu.age = 23; stu.sayName(); stu.showAge(); alert(stu instanceof User);//true alert(stu instanceof Student);//true alert(stu.constructor); //function User().... //缺点:不支持多重继承,构造函数不能有参数 */ /** * 5.继承:混合模式 = 构造函数 + 原型链方式 */ /* function User(name) { this.name = name; } User.prototype.sayName = function() { alert(this.name); } function Student(name,age) { //User.call(this,name); this.age = age; } //将User中所有通过prototype设置的方法放到Student中 Student.prototype = new User(); Student.prototype.showAge = function() { alert(this.age); } //调用 var stu = new Student(); stu.name = "tom"; stu.age = 23; stu.sayName(); stu.showAge(); alert(stu instanceof User);//true alert(stu instanceof Student);//true alert(stu.constructor); //function User(name) {this.name = name;} */ function User(name) { this.name = name; } User.prototype.sayName = function() { alert(this.name); } function Student(name,age) { //User.call(this,name); //this.age = age; } //将User中所有通过prototype设置的方法放到Student中 Student.prototype = new User(); Student.prototype.age = 0; Student.prototype.showAge = function() { alert(this.age); } //调用 var stu = new Student(); stu.name = "tom"; stu.age = 23; stu.sayName(); stu.showAge(); alert(stu instanceof User);//true alert(stu instanceof Student);//true alert(stu.constructor); //function User(name) {this.name = name;} })(); </script> </body> </html>
本文出自 “我的JAVA世界” 博客,请务必保留此出处http://hanchaohan.blog.51cto.com/2996417/1309679
相关文章推荐
- Javascript对象学习1 - 创建对象的几种方法
- js屏蔽鼠标右键 .net 禁止选择文本、shift、alt、ctrl
- javascript快速模板渲染引擎实现mvc
- JS实现时间格式化
- 9 行 javascript 代码获取 QQ 群成员
- js模仿hover
- 9 行 javascript 代码获取 QQ 群成员
- knockoutjs visible
- 使用 Web Tracing Framework 分析富 JS 应用的性能
- JSON-lib框架,转换JSON、XML不再困难
- xStream完美转换XML、JSON
- Internet Explorer 无法启用 JavaScript 怎么办?
- 利用CSS、JavaScript及Ajax实现图片预加载的三大方法(转)
- 利用CSS、JavaScript及Ajax实现图片预加载的三大方法
- 动态加载js脚本的方法
- 动态输出的javascript中alert文本的换行问题
- js判断文档上传
- 屏蔽webBrowser中JS加载错误
- 用JS实现点击TreeView根节点复选框全选
- [原创] JavaScript 图片放大镜插件 enlarge.js 以及移动版 enlarge.touch.js