面向對象與原型
function Box(){}
Box.prototype.name='Tayle';
Box.prototype.age='25';
Box.prototype.run=function(){
return this.name+this.age+'運行中......';
};
function Box(){}
Box.prototype={
name:'tayler',
age:'25',
run:function{
return this.name+this.age+'運行中......';
}
}
var box1= new Box();
box1.name='Jack';
alert(box1.name);
delete box1.name;
delete Box.prototype.name;
Box.prototype.name='KKK';
alert(box1.name) ;
原型的執行流程:
1.先瀏覽構造函數裏面屬性或者方法,如果有,立即返回;
2.如果構造函數裏面沒有,就去它的原型裏面找,如果有,就返回。
function Box () {}
Box.prototype={
constructor:Box,
name:'Lee',
age:'22',
run:function () {
return this.name+this.age+'運行中.....';
}
};
Box.prototype={
age:'23'
}
var box=new Box();
alert(box.name)
alert(box.age)
alert(box.run());
var box=[5,2,4,6,8];
alert(box.sort());
alert(Array.prototype.sort);
alert(String.prototype.addstring);
String.prototype.addstring=function () {
return this +',被添加了';
}
alert('Lee'.addstring());
unction Box () {}
Box.prototype={
name:'Li',
age:23,
family:['哥哥','姐姐','妹妹'],
run:function(){
return this.name+this.age+"運行....";
}
};
var box1= new Box();
alert(box1.family);
box1.family.push('弟弟');
alert(box1.family);
var box2 = new Box();
alert(box2.family);
- 混合模式(構造函數+原型模式) //混合模式很好的解決傳參和引用共享的,是解決對象比較好的方法
function Box (name,age) {
this.name=name;
this.age=age;
this.family=['哥哥','姐姐','妹妹'];
}
Box.prototype={
constructor:Box,
run:function(){
return this.name+this.age+"運行中";
}
};
var box1=new Box('LEE','22');
alert(box1.family);
box1.family.push('弟弟');
alert(box1.family);
var box2= new Box('kkk','23');
alert(box2.family);
function Box(name,age){
this.name=name;
this.age=age;
this.family=['哥哥','姐姐','妹妹'];
if(typeof this.run!='function'){
alert('原型初始化開始');
Box.prototype.run=function(){
return this.name+this.age+'運行中';
}
alert('原型初始化結束');
}
}
var box1=new Box('LEE','22');
alert(box1.run());
var box2= new Box('kkk','23');
alert(box2.run());
function Box(){
this.name='Lee';
}
function Desk(){
this.age='20';
}
function Table(){
this.level='aaa';
}
Desk.prototype = new Box()
var desk = new Desk();
alert(desk.name);
alert(desk.age);
Table.prototype = new Desk();
var table= new Table();
alert(table.name);
alert(table.age);
alert(desk instanceof Object)
function Box(name,age,family){
this.name=name;
this.age=age;
this.family=['哥哥','姐姐','妹妹'];
}
function Desk(name,age){
Box.call(this,name,age);
}
var desk = new Desk('kkk','23');
alert(desk.name)
alert(desk.family);
desk.family.push('弟弟');
alert(desk.family)
- 函數的繼承三(組合繼承:對象冒充+原型鏈,解決原型中方法訪問不到的問題)
function Box (name,age,family) {
this.name=name;
this.age=age;
this.family=['哥哥','姐姐','妹妹'];
}
Box.prototype.run=function (){
return this.name+this.age+'運行中...';
}
function Desk(name,age){
Box.call(this,name,age);
}
var desk = new Desk('Lee','22');
Desk.prototype = new Box();
alert(desk.run());
function obj (o) {
function F () {}
F.prototype=o;
return new F();
}
var box={
name:'jack',
age:100,
};
var box1= obj (box);
alert(box1.name);
function obj (o) {
function F () {}
F.prototype=o;
return new F();
}
function create (o) {
var f= obj(o);
f.run=function(){
return this.name+'方法';
}
return f;
}
var box={
name:'jack',
age:100,
family:['哥哥','姐姐','妹妹']
};
var box1= create(box);
alert(box1.run());
function obj (o) {
function F () {}
F.prototype=o;
return new F();
}
function create (box,desk) {
var f= obj(box.prototype);
f.constructor=desk;
desk.prototype=f;
}
function Box(name,age){
this.name=name;
this.age=age;
}
Box.prototype.run=function () {
return this.name+this.age+'運行中。。。';
}
function Desk(name,age){
Box.call(this,name,age);
}
create(Box,Desk);
var desk = new Desk('KKK','25');
alert(desk.run());
alert(desk.constructor);