JS 面向对象详解
面向对象详解1
OO1.html
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8" /> 5 <title></title> 6 </head> 7 <body> 8 <script src="js/app1.js"></script> 9 </body> 10 </html>
js/app1.js
1 /*function People(){ 2 3 } 4 People.prototype.say=function(){ 5 alert("hello"); 6 } 7 function Student(){ 8 9 } 10 Student.prototype=new People(); 11 var superSsay=Student.prototype.say; 12 Student.prototype.say=function(){ 13 superSsay.call(this); 14 alert("stu-hello"); 15 } 16 var s=new Student(); 17 s.say();*/ 18 19 20 /*function People(name){ 21 this._name=name; 22 } 23 People.prototype.say=function(){ 24 alert("peo-hello"+this._name); 25 } 26 function Student(name){ 27 this._name=name; 28 } 29 Student.prototype=new People(); 30 var superSsay=Student.prototype.say; 31 Student.prototype.say=function(){ 32 superSsay.call(this); 33 alert("stu-hello"+this._name); 34 } 35 var s=new Student("iwen"); 36 s.say();*/ 37 38 39 (function(){ 40 var n="ime"; 41 function People(name){ 42 this._name=name; 43 } 44 People.prototype.say=function(){ 45 alert("peo-hello"+this._name); 46 } 47 window.People=People; 48 }()); 49 50 51 (function(){ 52 function Student(name){ 53 this._name=name; 54 } 55 Student.prototype=new People(); 56 var superSsay=Student.prototype.say; 57 Student.prototype.say=function(){ 58 superSsay.call(this); 59 alert("stu-hello"+this._name); 60 } 61 window.Student=Student; 62 }()); 63 64 var s=new Student("iwen"); 65 s.say();
面向对象详解2
OO2.html
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title></title> 6 </head> 7 <body> 8 <script src="js/app2.js"></script> 9 </body> 10 </html>
js/app2.js
1 /*function Person(){ 2 var _this={}; 3 _this.sayHello=function(){ 4 alert("P-hello"); 5 } 6 return _this; 7 } 8 function Teacher(){ 9 var _this=Person(); 10 var surperSay=_this.sayHello; 11 _this.sayHello=function(){ 12 surperSay.call(_this); 13 alert("T-hello"); 14 } 15 return _this; 16 17 } 18 var t=Teacher(); 19 t.sayHello(); 20 */ 21 22 23 /*function Person(name){ 24 var _this={}; 25 _this._name=name; 26 _this.sayHello=function(){ 27 alert("P-hello"+_this._name); 28 } 29 return _this; 30 } 31 function Teacher(name){ 32 var _this=Person(name); 33 var surperSay=_this.sayHello; 34 _this.sayHello=function(){ 35 surperSay.call(_this); 36 alert("T-hello"+_this._name); 37 } 38 return _this; 39 40 } 41 var t=Teacher("iwen"); 42 t.sayHello();*/ 43 44 (function(){ 45 var n="ime"; 46 function Person(name){ 47 var _this={}; 48 _this._name=name; 49 _this.sayHello=function(){ 50 alert("P-hello"+_this._name+n); 51 } 52 return _this; 53 } 54 window.Person=Person; 55 }()); 56 function Teacher(name){ 57 var _this=Person(name); 58 var surperSay=_this.sayHello; 59 _this.sayHello=function(){ 60 surperSay.call(_this); 61 alert("T-hello"+_this._name); 62 } 63 return _this; 64 65 } 66 var t=Teacher("iwen"); 67 t.sayHello();
本文来自博客园,作者:一纸年华,转载请注明原文链接:https://www.cnblogs.com/nullcodeworld/p/9314153.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)