javascript之创建对象
学过面向对象编程的同学一定都知道,类是对象的模板,对象是根据模板创建的。
可是Javascript中没有类的概念,只有对象的概念,没有类怎么创建对象呢?
下面介绍两种创建javascript对象方法:
赋值法
1 2 3 4 5 6 7 8 9 10 11 12 13
| var shopProduct = { title:'上衣', "brand":"优衣库", 'price':300, getSunmary:function(){ return "titile: "+this.title+", brand: "+this.brand+", price: "+this.price; } }
console.log(shopProduct.title); console.log(shopProduct.price); console.log(shopProduct.brand);
|
构造函数法
先定义一个函数,然后用关键字new调用它,就会得到一个对象。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| function ShopProduct(name,brand,price) { this.name =name; this.brand= brand; this.price = price; this.getSunmary = function(){ return "titile: "+this.name+", brand: "+this.brand+", price: "+this.price; };
return this;
}
shoes = new ShopProduct('shoes','nike',500);
console.log(shoes.name); console.log(shoes.price); console.log(shoes.brand); console.log(shoes.getSunmary());
|
注意:
- 构造函数中的this指向新对象。
- 构造函数中虽然没有return,但是默认返回this。