js繼承

1.繼承有什么作用?

繼承是指一個(gè)對(duì)象直接使用另一個(gè)對(duì)象是屬性和方法。
作用:繼承可以使子類(lèi)共享父類(lèi)的屬性和方法。
繼承可以覆蓋或擴(kuò)展父類(lèi)的屬性和方法。

2.有幾種常見(jiàn)創(chuàng)建對(duì)象的方式? 舉例說(shuō)明?

1.簡(jiǎn)單對(duì)象字面量

var obj1 = {};

2.工廠模式

function Person(name,age){
  var obj = {
    name: name,
    age: age
  }
  return obj;
}
var Person1 = Person("Li",18);
var Person2 = Person("Xin",18);

3.構(gòu)造函數(shù)

function Person(name,age){
  this.name = name;
  this.age = age;
}
var Person1 = new Person("Yun",20);

4.原型+構(gòu)造函數(shù)

function Person(name,age){
  this.name = name;
  this.age = age;
}
Person.prototype.sayName = function(){
  console.log("name is " + this.name)
}
var Person1 = new Person("Jing",24);
Person1.sayName();

3.下面兩種寫(xiě)法有什么區(qū)別?

//方法1
function People(name, sex){
    this.name = name;
    this.sex = sex;
    this.printName = function(){
        console.log(this.name);
    }
}
var p1 = new People('Li', 2)

//方法2
function Person(name, sex){
    this.name = name;
    this.sex = sex;
}

Person.prototype.printName = function(){
    console.log(this.name);
}
var p1 = new Person('Yun', 27);

第一種:直接將對(duì)象的屬性和方法都創(chuàng)建在了構(gòu)造函數(shù)上,沒(méi)創(chuàng)建一個(gè)對(duì)象實(shí)例,都要重復(fù)創(chuàng)建該對(duì)象的方法,造成資源浪費(fèi),比較耗內(nèi)存。
第二種:對(duì)象的屬性創(chuàng)建在了構(gòu)造函數(shù)上,而方法寫(xiě)在了原型prototype上,實(shí)例對(duì)象可通過(guò)原型鏈直接調(diào)用該方法,實(shí)現(xiàn)了方法共享,節(jié)省了內(nèi)存空間。

4.Object.create 有什么作用?兼容性如何?如何使用?

Object.create可以創(chuàng)建一個(gè)擁有指定原型和若干個(gè)指定屬性的對(duì)象。
兼容性:IE9+,Chrome5+,Firfox4.0+,Opera11.60+,Safari5+。
使用方法:Object.craete(prototype,[])

   function Person(name){
     this.name = name;
   }
   Person.prototype.sayName = function(){
     console.log("name is " + this.name);
   }
   function People(){
   }
   People.prototype =  Object.create(Person.prototype);
   var p1 = new Person("li");

5.hasOwnProperty有什么作用? 如何使用?

作用:返回一個(gè)布爾值,判斷某個(gè)對(duì)象是否含有指定的自身屬性,而非其原型鏈上繼承到的屬性。
語(yǔ)法:obj.hasOwnProperty(prop),prop為要檢測(cè)的屬性

  p1.hasOwnProperty('age');

6.實(shí)現(xiàn)Object.create的 polyfill,如:(ps: 寫(xiě)個(gè) 函數(shù)create,實(shí)現(xiàn) Object.create 的功能)

function create(obj){
  function temp(){};
  if(typeof obj !== 'object'){
    console.log('error')
  } else {
    temp.prototype = obj;
    var newTemp = new temp();
    return newTemp;
  }
}

var obj = {a: 1, b:2};
var obj2 = create(obj);
console.log(obj2.a); //1

7.如下代碼中call的作用是什么?

function Person(name, sex){
    this.name = name;
    this.sex = sex;
}
function Male(name, sex, age){
    Person.call(this, name, sex);   //call的作用,在Male作用域中調(diào)用Person,使Male能夠執(zhí)行Person函數(shù)。
    this.age = age;
}

8.實(shí)現(xiàn)繼承

function Person(name, sex){
    this.name = name;
    this.sex = sex;
}

Person.prototype.getName = function(){
    console.log('name is ' + this.name);
};    

function Male(name, sex, age){
   this.age = age;
   Person.call(this,name, sex);
}
Male.prototype = Object.create(Person.prototype);
Male.prototype.constructor = Male;
Male.prototype.getAge = function(){
    console.log('age is ' + this.age);
};

var ruoyu = new Male('ruoyu', '男', 27);
ruoyu.getAge();
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容