面向对象与原型
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);