1.繼承有什么作用?
使用現(xiàn)有類的所有功能醒叁,并在無需重新編寫原來的類的情況下對這些功能進行擴展。
2.有幾種常見創(chuàng)建對象的方式? 舉例說明?
(1)工廠模式
function createPerson(name, age){
var o = new Object();
o.name = name;
o.age = age;
o.sayName = function(){
console.log('my name is ' + this.name);
}
return o;
}
var p1 = createPerson('hunger', 20);
var p2 = createPerson('velly', 30);
p1.sayName();//my name is hunger
p2.sayName();//my name is velly
(2)構造函數模式
function Person(name, age){
this.name = name;
this.age = age;
this.sayName = function(){
console.log(this.name);
}
}
var p1 = new Person('hunger', 20);
var p2 = new Person('velly', 30);
p1.sayName();//hunger
p2.sayName();//velly
(3)原型模式
function Person(){
}
Person.prototype = {
construtor: Person;
name: 'hunger';
age: 20;
friends: ['velly', 'jiregu'];
this.sayName: function(){
console.log(this.name);
}
}
var p1 = new Person();
var p2 = new Person();
p1.friends.push('jrg');
console.log(p1.friends);//'velly, jirengu,jrg'
console.log(p2.friends);//'velly,jirengu,jrg'
(4)組合使用構造函數模式和原型模式:
function Person(name, age){
this.name = name;
this.age = age;
}
Person.prototype.sayName = function(){
console.log(this.name);
}
var p1 = new Person('hunger', 20);
var p2 = new Person('jirengu', 30);
p1.sayName();//hunger
p2.sayName();//jirengu
3.下面兩種寫法有什么區(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ū)別:同樣都是創(chuàng)建printName方法嘹黔,方法1的printName方法是在函數Person實例對象里的,方法2是在Person的prototype對象上的署惯。當再創(chuàng)建一個Person實例對象的時候行施,方法1又將會再創(chuàng)建一個printName方法,占用新的內存若锁,而方法2將一個公用的printName方法寫在原型上搁骑,當對象要使用該方法只需到原型鏈里調用就可以了,達到節(jié)省內存的效果又固。
4.Object.create
有什么作用仲器?兼容性如何?如何使用仰冠?
- 作用:創(chuàng)建一個擁有指定原型和若干個指定屬性的對象乏冀。
-
兼容性:
兼容性 - 使用:
function Person(name, age){
this.name = name;
this.age = age;
}
Person.prototype.sayName = function(){
console.log(this.name);
}
function Male(name, age, sex){
Person.call(this, name, age);
this.sex = sex;
}
Male.prototype = Object.create(Person.prototype);
Male.prototype.saySex = function(){
console.log(this.sex);
}
var p1 = new Male('hunger', 20, 'nan');
p1.saySex();
5.hasOwnProperty
有什么作用? 如何使用洋只?
- 作用:判斷一個對象是否包含自定義屬性而不是原型鏈上的屬性
- 語法:
obj.hasOwnProperty(prop)
(prop為要檢測的屬性名稱)
-使用:
p1.hasOwnProperty('name');//true
p1.hasOwnProperty('sayName');//false
p1.hasOwnProperty.prototype('saySex');//true
6.實現(xiàn)Object.create
的 polyfill辆沦,如:(ps: 寫個 函數create,實現(xiàn) Object.create 的功能)
function create(obj){
function Temp(){}
Temp.prototype = obj;
return new Temp();
}
var obj = {a: 1, b:2};
var obj2 = create(obj);
console.log(obj2.a); //1
7.如下代碼中call的作用是什么?
unction Person(name, sex){
this.name = name;
this.sex = sex;
}
function Male(name, sex, age){
Person.call(this, name, sex);
//這里的call是獲取構造函數Person的屬性识虚,把Person的環(huán)境改到自己(Male)的作用域內肢扯,從而實現(xiàn)構造函數的繼承。
this.age = age;
}
8.補全代碼担锤,實現(xiàn)繼承
function Person(name, sex){
this.name = name;
this.sex= sex;
}
Person.prototype.getName = 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;
};
Male.prototype.printName = function(){
console.log(this.name);
}
var ruoyu = new Male('若愚', '男', 27);
ruoyu.printName();