1.創(chuàng)建對象,可以通過函數(shù)來創(chuàng)建一個對象
function dog(){
/*1.1創(chuàng)建一個對象*/
var obj = new Object();
/*1.2設(shè)置屬性*/
obj.name = '小花';
obj.age = 0;
obj.dogFrends = ['小黃','小白'];
obj.eat = function(){
console.log('吃牛奶');
}
obj.run = function(){
console.log('我家');
}
/*返回這個對象*/
return obj;
}
var smallDog = dog();
console.log(smallDog.name);
var bigDog = dog();
console.log(bigDog.age);
1.創(chuàng)建對象,可以通過函數(shù)來創(chuàng)建一個對象
function dog(name,age,dogFriends){
/*1.1創(chuàng)建一個對象*/
var obj = new Object();
/*1.2設(shè)置屬性*/
obj.name = name;
obj.age = age;
obj.dogFrends = dogFriends;
obj.eat = function(){
console.log('吃牛奶');
}
obj.run = function(){
console.log('我家');
}
/*返回這個對象*/
return obj;
}
var smallDog = dog('小花',0,['小白','小黃']);
console.log(smallDog.age);
var bigDog = dog('大花',2,['大白','大黃']);
console.log(bigDog.age);
1.創(chuàng)建對象,可以通過函數(shù)來創(chuàng)建一個對象
function dog(name,age,dogFriends){
/*1.1創(chuàng)建一個對象*/
var obj = new Object();
/*1.2設(shè)置屬性*/
this.name = name;
this.age = age;
this.dogFrends = dogFriends;
this.eat = function(){
console.log('吃牛奶');
}
this.run = function(){
console.log('我家');
}
/*返回這個對象*/
return obj;
}
var smallDog = dog('小花',0,['小白','小黃']);
console.log(smallDog.age);
var bigDog = dog('大花',2,['大白','大黃']);
console.log(bigDog.age);
- 通過這種方式創(chuàng)建地對象可以作為模板,但是我們創(chuàng)建的過程需要創(chuàng)建一個對象淹遵,而且每次都需要返回這個對象,所以我們需要改造這個方法口猜。
- 4.使用構(gòu)造函數(shù)創(chuàng)建對象:
1.創(chuàng)建對象,可以通過構(gòu)造函數(shù)來創(chuàng)建一個對象
function Dog(name,age,dogFriends){
/*1.2設(shè)置屬性*/
this.name = name;
this.age = age;
this.dogFrends = dogFriends;
this.eat = function(someThing){
console.log( this.name+'吃'+someThing);
}
this.run = function(someWhere){
console.log(this.name +'跑'+someWhere);
}
}
var smallDog = new Dog('小花',0,['小白','小黃']);
console.log(smallDog.age);
var bigDog = new Dog('大花',2,['大白','大黃']);
console.log(bigDog.age);
smallDog.eat('奶');
bigDog.eat('牛肉');
- 5.使用構(gòu)造函數(shù)創(chuàng)建對象優(yōu)化參數(shù)
因為參數(shù)較多的時候,我們寫起來不容易透揣,所以我們抽取一個用來存儲參數(shù)的數(shù)據(jù)济炎,這個數(shù)據(jù)一般是字典。
1.創(chuàng)建對象,可以通過構(gòu)造函數(shù)來創(chuàng)建一個對象
function Dog(option){
/*1.0判斷是否傳入字典*/
var option = option ||{};
/*1.1設(shè)置屬性*/
this.name = option.name;
this.age = option.age;
this.dogFrends = option.dogFrends;
this.eat = function(someThing){
console.log( this.name+'吃'+someThing);
}
this.run = function(someWhere){
console.log(this.name +'跑'+someWhere);
}
}
var smallDog = new Dog({name:'小花',age:0,dogFriends:['小白','小黃']});
console.log(smallDog.age);
var bigDog = new Dog({name:'大花',age:2,dogFriends:['大白','大黃']});
console.log(bigDog.age);
smallDog.eat('奶');
bigDog.eat('牛肉');
alert(smallDog.eat == bigDog.eat); //false
- 通過購造函數(shù)創(chuàng)建出來的對象每一個都是一個新的對象
- 他們里面的同名方法不是一樣的
- 但是我們這些方法實現(xiàn)的功能是一樣的
- 所以我們沒有必要每次創(chuàng)建一個對象都設(shè)置方法
- 最好辐真,讓這些創(chuàng)建的對象擁有的是同樣的方法
- 6.使用原型對象
1.創(chuàng)建對象,可以通過構(gòu)造函數(shù)來創(chuàng)建一個對象
function Dog(option){
/*1.0判斷是否傳入字典*/
var option = option ||{};
/*1.1設(shè)置屬性*/
this.name = option.name;
this.age = option.age;
this.dogFrends = option.dogFrends;
}
Dog.prototype = {
eat:function(someThing){
console.log( this.name+'吃'+someThing);
},
run:function(someWhere){
console.log(this.name +'跑'+someWhere);
}
}
var smallDog = new Dog({name:'小花',age:0,dogFriends:['小白','小黃']});
console.log(smallDog.age);
var bigDog = new Dog({name:'大花',age:2,dogFriends:['大白','大黃']});
console.log(bigDog.age);
smallDog.eat('奶');
bigDog.eat('牛肉');
/*1.創(chuàng)建對象,可以通過構(gòu)造函數(shù)來創(chuàng)建一個對象
function Dog(option){
/*當(dāng)使用對應(yīng)的構(gòu)造函數(shù)創(chuàng)建對象的時候须尚,我們首先調(diào)用初始化函數(shù)*/
this._init(option);
}
//設(shè)置原型對象
Dog.prototype = {
/*如果放置屬性一般通過初始化方法來放置屬性*/
_init:function(option){
/*1.1設(shè)置屬性*/
var option = option ||{};
this.name = option.name;
this.age = option.age;
this.dogFrends = option.dogFrends;
},//設(shè)置方法
eat:function(someThing){
console.log( this.name+'吃'+someThing);
},
run:function(someWhere){
console.log(this.name +'跑'+someWhere);
}
}
var smallDog = new Dog({name:'小花',age:0,dogFriends:['小白','小黃']});
console.log(smallDog.age);
var bigDog = new Dog({name:'大花',age:2,dogFriends:['大白','大黃']});
console.log(bigDog.age);
smallDog.eat('奶');
bigDog.eat('牛肉');
- 8.this的認(rèn)識
- this表示的意義:
this一般放在函數(shù)(方法)中,如果哪個對象擁有這個函數(shù)(哪個對象調(diào)用這個函數(shù))那么this就表示這個對象
所有的全局變量和方法都是window的屬性和方法
function dog(){
alert(this);
}
dog();
// 任何一個函數(shù)動都可以通過new來創(chuàng)建一個對象侍咱,所以我們這個dog函數(shù)屬于新創(chuàng)建的對象耐床,所以這個this表示新創(chuàng)建地對象.
new dog();
- this的特殊的用法:
如果this在事件指令中,表示事件觸發(fā)者楔脯,一般是事件源
如果this在定時器中撩轰,表示window