繼承有什么作用? (難度:***
)
- 得到一個(gè)類的屬性
- 得到一個(gè)類的方法
有幾種常見創(chuàng)建對象的方式? 舉例說明? (難度:****
)
- 直接賦值
var obj = new Object
或者var obj = {};
- 工廠模式
function createCake(name, age){
var obj = {
name: name,
sayName: function(){
console.log('我是' + this.name);
}
}
return obj;
}
var cake1 = createCake('草莓味面包');
var cake2 = createCake('巧克力面包'); - 構(gòu)造函數(shù)模式
function Cake(name) {
this.name = name;
this.sayName = function(){
console.log('我是' + this.name);
}
}
var cake1 = new Cake('草莓味面包'),
cake2 = new Cake('巧克力面包');
console.log( cake1 instanceof Cake ); //true
console.log( cake2 instanceof Cake ); //true
下面兩種寫法有什么區(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);
方法一每創(chuàng)建一個(gè)實(shí)例,都會(huì)將printName綁定在實(shí)例的私有空間內(nèi);而方法二函數(shù)將printName放在prototype內(nèi),該函數(shù)構(gòu)建的實(shí)例共用方法printName,減少代碼量。
Object.create 有什么作用?兼容性如何?如何使用? (難度:***
)
- Object.create() 方法創(chuàng)建一個(gè)擁有指定原型和若干個(gè)指定屬性的對象
Object.create(proto, [ propertiesObject ])
例子:Male.prototype = Object.create(Person.prototype);
- ES5新特性,需要IE9及以上的版本
hasOwnProperty有什么作用? 如何使用? (難度:***
)
- hasOwnPerperty是Object.prototype的一個(gè)方法,可以判斷一個(gè)對象是否包含自定義屬性而不是原型鏈上的屬性,hasOwnProperty是JavaScript中唯一一個(gè)處理屬性但是不查找原型鏈的函數(shù)
m.hasOwnProperty('name'); // true
m.hasOwnProperty('printName'); // false
Male.prototype.hasOwnProperty('printAge'); // true
實(shí)現(xiàn)Object.create的 polyfill,如:(ps: 寫個(gè) 函數(shù)create,實(shí)現(xiàn) Object.create 的功能) (難度:****)
if (typeof Object.create != 'function'){
Obj.create = (function(){
function Temp() {}
var hasOwn = Object.prototype.hasOwnProperty;
return function(proto){
if (typeof proto != 'object'){
throw TypeError('Object prototype may only be an Object or null');
}
Temp.prototype = proto;
var obj = new Temp();
Temp.prototype = null;
if (arguments.length > 1){
var Properties = Object(arguments[1]);
for (var prop in Properties){
if (hasOwn.call(Properties, prop)) {
obj[prop] = Properties[prop];
}
}
}
return obj;
};
})();
}
如下代碼中call的作用是什么? (難度:****
)
function Person(name, sex){
this.name = name;
this.sex = sex;
}
function Male(name, sex, age){
Person.call(this, name, sex); //改變this指向?qū)崿F(xiàn)實(shí)例的繼承。
this.age = age;
}
補(bǔ)全代碼,實(shí)現(xiàn)繼承 (難度:****
)
function Person(name, sex){
// todo ...
}
Person.prototype.getName = function(){
// todo ...
};
function Male(name, sex, age){
//todo ...
}
//todo ...
Male.prototype.getAge = function(){
//todo ...
};
var ruoyu = new Male('若愚', '男', 27);
ruoyu.printName();
function Person(name, sex){
this.name = name;
this.sex = sex;
}
Person.prototype.getName = function(){
console.log(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(){
console.log(this.age)
};
Male.prototype.printName=function(){
console.log(this.name);
};
var ruoyu = new Male('若愚', '男', 27);
ruoyu.printName();