JS 面向对象详解

面向对象详解1

OO1.html

 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<script src="js/app1.js"></script>
</body>
</html>

js/app1.js

 /*function People(){

 }
People.prototype.say=function(){
alert("hello");
}
function Student(){ }
Student.prototype=new People();
var superSsay=Student.prototype.say;
Student.prototype.say=function(){
superSsay.call(this);
alert("stu-hello");
}
var s=new Student();
s.say();*/ /*function People(name){
this._name=name;
}
People.prototype.say=function(){
alert("peo-hello"+this._name);
}
function Student(name){
this._name=name;
}
Student.prototype=new People();
var superSsay=Student.prototype.say;
Student.prototype.say=function(){
superSsay.call(this);
alert("stu-hello"+this._name);
}
var s=new Student("iwen");
s.say();*/ (function(){
var n="ime";
function People(name){
this._name=name;
}
People.prototype.say=function(){
alert("peo-hello"+this._name);
}
window.People=People;
}()); (function(){
function Student(name){
this._name=name;
}
Student.prototype=new People();
var superSsay=Student.prototype.say;
Student.prototype.say=function(){
superSsay.call(this);
alert("stu-hello"+this._name);
}
window.Student=Student;
}()); var s=new Student("iwen");
s.say();

面向对象详解2

OO2.html

 <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script src="js/app2.js"></script>
</body>
</html>

js/app2.js

 /*function Person(){
var _this={};
_this.sayHello=function(){
alert("P-hello");
}
return _this;
}
function Teacher(){
var _this=Person();
var surperSay=_this.sayHello;
_this.sayHello=function(){
surperSay.call(_this);
alert("T-hello");
}
return _this; }
var t=Teacher();
t.sayHello();
*/ /*function Person(name){
var _this={};
_this._name=name;
_this.sayHello=function(){
alert("P-hello"+_this._name);
}
return _this;
}
function Teacher(name){
var _this=Person(name);
var surperSay=_this.sayHello;
_this.sayHello=function(){
surperSay.call(_this);
alert("T-hello"+_this._name);
}
return _this; }
var t=Teacher("iwen");
t.sayHello();*/ (function(){
var n="ime";
function Person(name){
var _this={};
_this._name=name;
_this.sayHello=function(){
alert("P-hello"+_this._name+n);
}
return _this;
}
window.Person=Person;
}());
function Teacher(name){
var _this=Person(name);
var surperSay=_this.sayHello;
_this.sayHello=function(){
surperSay.call(_this);
alert("T-hello"+_this._name);
}
return _this; }
var t=Teacher("iwen");
t.sayHello();
上一篇:udp 局域网群聊


下一篇:【剑指offer】求树中满足和为给定数字的路径