<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JS的對象</title>
<script>
// this所在的函數(shù)在哪個對象中,this就代表這個對象
// 對象
var dog = {
name: 'wangCai',
age: 18,
height: 166,
dogFriends: ['laiFu', 'aHuang'],
run: function (someWhere) {
console.log(this.name + '在跑----' + someWhere);
},
eat: function (someThing) {
console.log(this.name + '在吃----' + someThing);
}
};
console.log(dog.name, dog.dogFriends);
dog.run('杭州下沙');
dog.eat('五花肉');
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script>
function Dog() {
console.log('我是一個普通函數(shù)');
}
// 調(diào)用函數(shù)
// Dog();
// 升級 ---> 構(gòu)造函數(shù) alloc init new
var dog1 = new Dog();
var dog2 = new Dog();
console.log(dog1, dog2);
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>驗證批量產(chǎn)生對象</title>
<script>
// 構(gòu)造函數(shù)
function Dog() {
this.name = null;
this.age = null;
this.height = null;
this.dogFriends = [];
this.eat = function (someThing) {
console.log(this.name + '吃' + someThing);
};
this.run = function (someWhere) {
console.log(this.name + '跑' + someWhere);
}
}
// 產(chǎn)生對象
var dog1 = new Dog();
var dog2 = new Dog();
dog1.name = 'lili';
dog1.age = 18;
dog1.dogFriends = ['232', '007'];
dog2.name ='wangcai';
dog2.age = 1;
dog2.height = 0.22;
console.log(dog1, dog2);
dog1.eat('骨頭');
dog2.eat('奶');
// 帶參數(shù)
function OtherDog(name, age, height, dogFriends) {
this.name = name;
this.age = age;
this.height = height;
this.dogFriends = dogFriends;
this.eat = function (someThing) {
console.log(this.name + '吃' + someThing);
};
this.run = function (someWhere) {
console.log(this.name + '跑' + someWhere);
}
}
var dog3 = new OtherDog('w', 19, 1122, ['2121']);
console.log(dog3);
</script>
</head>
<body>
</body>
</html>
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。