繼承有什么作用? (難度:***)
繼承是面向?qū)ο缶幊痰恼Z言中擁有的特性, 繼承可以提高代碼的復(fù)用性, 基于類的面向?qū)ο笳Z言通過繼承可以使子類繼承父類的屬性和方法, 但是JavaScript中沒有類的概念, JavaScript中通過使用原型鏈實(shí)現(xiàn)繼承
有幾種常見創(chuàng)建對象的方式? 舉例說明? (難度:****)
- 工廠模式
缺點(diǎn): 無法知道對象的類型
function createPerson(name, age, job) {
var o = new Object()
o.name = name
o.age = age
o.job = job
return o
}
var p1 = createPerson('Tom', 18, 'Engineer')
- 構(gòu)造函數(shù)模式
缺點(diǎn): 每個(gè)方法在每個(gè)實(shí)例上都重新創(chuàng)造一遍
function Person(name, age, job) {
this.name = name
this.age = age
this.job = job
this.sayName = function() {
console.log(this.name)
}
}
var p = new Person('james', 18, 'Engineer')
- 原型模式
缺點(diǎn): 所有屬性和方法都被實(shí)例共享
function Person() {
}
Person.prototype.name = 'Tom'
Person.prototype.age = 18
Person.prototype.job = 'Engineer'
Person.prototype.sayName = function(){
console.log(this.name)
}
var p1 = new Person()
- 構(gòu)造函數(shù)和原型模式組合使用
ECMAscript中使用最廣的模式
function Person(name, age, job) {
this.name = name
this.age = age
this.job = job
}
Person.prototype.sayName = function(){
console.log(this.name)
}
var p1 = new Person('Tom', 18, 'Engineer')
下面兩種寫法有什么區(qū)別? (難度:***)
//方法1
function People(name, sex){
this.name = name;
this.sex = sex;
this.printName = function(){
console.log(this.name);
}
}
var p1 = new People('饑人谷', 2)
//方法2
function Person(name, sex){
this.name = name;
this.sex = sex;
}
Person.prototype.printName = function(){
console.log(this.name);
}
var p1 = new Person('若愚', 27);
方法一和方法二的區(qū)別主要是方法一的printName方法定義在構(gòu)造函數(shù)中, 使用的是構(gòu)造函數(shù)模式創(chuàng)建對象, 方法二的printName方法定義在原型上, 使用的是構(gòu)造函數(shù)和對象模式組合的方式創(chuàng)建對象, 因?yàn)榉椒ㄒ恢械膶懛〞?dǎo)致每個(gè)實(shí)例都創(chuàng)建一個(gè)新的printName方法, 但是他們的功能是一樣的, 這樣做是沒有必要的, 所以推薦方法二組合構(gòu)造函數(shù)和原型模式創(chuàng)建對象的方法, 將私有的屬性寫在構(gòu)造函數(shù)中, 公有的方法寫在原型對象上
Object.create 有什么作用?兼容性如何?如何使用? (難度:***)
- 作用
創(chuàng)建一個(gè)擁有指定原型和若干屬性的對象, 可以用來指定對象的原型 -
兼容性
Object.create()是ES5的屬性, 支持IE9及以上的版本
2016-09-25_221217.png - 如何使用
//模擬類實(shí)現(xiàn)繼承
function Animal(name) {
this.name = name
}
Animal.prototype.sayName = function(){
console.log(this.name)
}
function Cat(name) {
Animal.call(this, name)
}
Cat.prototype = Object.create(Animal.prototype)
Cat.prototype.run = function(){
console.log('run')
}
var cat = new Cat('tommy')
//改寫原型鏈直接實(shí)現(xiàn)繼承
var animal = {
say: function() {
console.log(this.name)
}
}
cat = Object.create(animal)
cat.name = 'Tommy'
hasOwnProperty有什么作用? 如何使用? (難度:***)
- hasOwnProperty作用
可以用來判斷一個(gè)是否是實(shí)例對象上的屬性, 返回一個(gè)布爾值 - 用法
obj.hasOwnProperty(prop)
var person = {
name: 'Tom'
}
person.hasOwnProperty('name')//true
person.hasOwnProperty('toString')//false
實(shí)現(xiàn)Object.create的 polyfill,如:(ps: 寫個(gè) 函數(shù)create,實(shí)現(xiàn) Object.create 的功能) (難度:****)
//polyfill
function create(obj) {
if (typeof Object.create !== 'function') {
var Temp = function(){}
Temp.prototype = obj
return new Temp()
}
return Object.create(obj)
}
//test
var obj = {a: 1, b:2};
var obj2 = create(obj);
console.log(obj2.a);
如下代碼中call的作用是什么? (難度:****)
function Person(name, sex){
this.name = name;
this.sex = sex;
}
function Male(name, sex, age){
Person.call(this, name, sex); //這里的 call 有什么作用
this.age = age;
}
使用call調(diào)用函數(shù)可以指定函數(shù)的this值, 因此, 可以把Person中的this值指定為使用Male構(gòu)造函數(shù)構(gòu)造出來的實(shí)例對象上, 實(shí)現(xiàn)構(gòu)造函數(shù)的繼承
補(bǔ)全代碼,實(shí)現(xiàn)繼承 (難度:****)
function Person(name, sex){
this.name = name
this.sex = sex
}
Person.prototype.printName = function(){
return this.name
};
function Male(name, sex, age){
Person.call(this,name,sex)
this.age = age
}
Male.prototype = Object.create(Person.prototype)
Male.prototype.constructor = Male
Male.prototype.getAge = function(){
return this.age
};
var ruoyu = new Male('若愚', '男', 27);
ruoyu.printName();
