1. 繼承有什么作用?
對代碼功能進行擴展,提高代碼復用性
2. 有幾種常見創(chuàng)建對象的方式? 舉例說明?
//直接創(chuàng)建
var obj = {
name: "jirengu",
age: 28
}
//使用構造函數(shù)
function Jrg(name,age){
this.name = name;
this.age = age;
}
var p2 = new Jrg("jirengu",28);
//函數(shù)工廠創(chuàng)建
function creatObj(name,age){
var obj = {
name = name;
age = age;
sayName = function(){
console.log(this.name)
}
}
return obj;
}
var obj = creatObj("jirengu",28);
var obj2 = creatObj("fangfang",24);
//原型方式
function obj(name,age){
obj.prototype.name = name;
obj.prototype.age = age;
obj.prototype.sayName = function(){
console.log(name);
};
}
var Obj1 = new obj("jirengu",28)
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);
方法1使用構造函數(shù)創(chuàng)建了對象p1游昼,該對象擁有p1.name甘苍、p1.sex和p1.printName
三個屬性
方法2將方法printName
綁定在對象Person的原型上,這樣烘豌,通過Person新定義的對象就都擁有了printName
方法载庭,提高了代碼復用性。
4. Object.create有什么作用廊佩?兼容性如何囚聚?如何使用?
對對象進行clone.ES5的方法罐寨,不兼容IE6、IE7
使用方法:
function Person(name,sex){
this.name = name;
this.sex = sex;
}
Person.prototype.sayName = function(){
console.log(this.name);
}
function Male(name,sex,age){
Person.call(this,name,sex);
this.age = age;
}
var _proto = Object.create(Person.prototype);
Male.prototype = _proto;
_proto.constactor = Male;
var p =new Male("nz","男","28");
p.sayName();
5. hasOwnProperty有什么作用序矩? 如何使用鸯绿?
hasOwnPerperty是Object.prototype的一個方法,可以判斷一個對象是否包含自定義屬性而不是原型鏈上的屬性,hasOwnProperty是JavaScript中唯一一個處理屬性但是不查找原型鏈的函數(shù)
以問題4中的代碼為例:
m.hasOwnProperty('name'); // true
m.hasOwnProperty('printName'); // false
Male.prototype.hasOwnProperty('printAge'); // true
6. 實現(xiàn)Object.create的 polyfill瓶蝴,如:(ps: 寫個 函數(shù)create毒返,實現(xiàn) Object.create 的功能)什么是 polyfill?
一個polyfill就是一個用在瀏覽器API上的shim.我們通常的做法是先檢查當前瀏覽器是否支持某個API,如果不支持的話就加載對應的polyfill.然后新舊瀏覽器就都可以使用這個API了
function create(obj){
if(typeof Object.create !== "function"){
var func = function(){
func.prototype = obj.prototype;
return new func();
}
}
return Object.create(obj.prototype);
}
var obj = {
a: 1,
b:2
};
var obj2 = create(obj);
console.log(obj2.a); //1
6. 如下代碼中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;
}
//在this的作用域下執(zhí)行Person
7. 補全代碼舷手,實現(xiàn)繼承
function Person(name, sex){
this.name = name;
this.sex = sex;
}
Person.prototype.getName = function(){
return this.name;
};
Person.prototype.printName = function(){
console.log(this.age);
}
function Male(name, sex, age){
Person.call(this,name,sex);
this.age = age;
}
var _proto = Object.create(Person.prototype);
Male.prototype = _proto;
Male.prototype.constractor = Male;
Male.prototype.getAge = function(){
return this.age;
};
var ruoyu = new Male('若愚', '男', 27);
ruoyu.printName();
本文版權歸簾外修竹及杭州饑人谷所有拧簸,轉載必須說明出處