继承:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script src="Lib.js"></script>
</head>
<body>
<!--script标签放在body还是head里面并没有什么区别 但是之前那个程序是因为外面有一个文件夹前面才写js/的啊……-->
</body>
</html>
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 superSay=Student.prototype.say;
Student.prototype.say= function () {
superSay.call(this);
alert("stu-hello"+this._name);
}
var s=new Student("iwen");
s.say();
封装:
(function () {
var n="iwens";//只能在当前类中访问
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 superSay=Student.prototype.say;
Student.prototype.say= function () {
superSay.call(this);
alert("stu-hello"+this._name);
}
window.Student=Student;
}())//想要他直接执行 所以加了()
var s=new Student("iwen");
s.say();
方便理解的类与对象赋值:
function Person(){
var _this={}//空对象
_this.sayHello=function(){
alert("hello");
}
return _this;
}
function Teacher(){
var _this=Person();
return _this;
}
var t=Teacher();
t.sayHello();
父类的复写与调用:
function Person(){
var _this={}//空对象
_this.sayHello=function(){
alert("Phello");
}
return _this;
}
function Teacher(){
var _this=Person();
var surperSay=_this.sayHello;
_this.sayHello=function(){
surperSay.call(_this);
alert("Thello");
}
return _this;
}
var t=Teacher();
t.sayHello();
参数传递:
function Person(name){
var _this={}//空对象
_this._name=name;
_this.sayHello=function(){
alert("Phello"+_this._name);
}
return _this;
}
function Teacher(name){
var _this=Person(name);
var surperSay=_this.sayHello;
_this.sayHello=function(){
surperSay.call(_this);
alert("Thello"+_this._name);
}
return _this;
}
var t=Teacher("iwen");
t.sayHello();
不就是改个字体 结果把我所有代码的缩进全弄没了==