js原型

通過(guò)js創(chuàng)建一個(gè)正方形

正方形

let square = {
width: 5,
getArea(){
  return this.width *  this.width
},
getLength(){
  return this.width * 4
}

}

接下來(lái)需要多個(gè)正方形滨溉?

let squareList = []
let widthList = [5,6,6,6,5,6,6,5,5,6]
for(let i = 0; i < 12; i++){
  squareList[i] = {
    width: widthList[i],
    getArea(){
      return this.width *  this.width
    },
    getLength(){
      return this.width * 4
    }
  }
}

浪費(fèi)太多內(nèi)存

image.png

image.png

解決方法什湘,使用原型

let squareList = [];
let widthList = [5,6,5,6,5,6,5,6,5,6]
let squarePrototype = {
    getArea(){
      return this.width *  this.width
    },
    getLength(){
      return this.width * 4
    }
}
for(let i = 0; i < 12; i++){
  squareList[i] = Object.create(squarePrototype)
  squareList[i].width = widthList[i]
}
image.png

抽離到函數(shù)

let squareList = []
let widthList = [5,6,5,6,5,6,5,6,5,6]
function createSquare(width){
  let obj = Object.create(squarePrototype)
  obj.width = width
  return obj
}
let squarePrototype = {
  getArea(){
    return this.width * this.width
  },
  getLength () {
    return this.width*4
  }
}
for(let i = 0; i < 12; i++){
  squareList[i] = createSquare(widthList[i])
}

函數(shù)和原型結(jié)合

let squareList = [];
let widthList = [5,6,5,6,5,6,5,6,5,6]
function createSquare(width){
  let obj = Object.create(createSquare.squarePrototype)
  obj.width = width
  return obj
}
createSquare.squarePrototype = {
  getArea(){
    return this.width * this.width
  },
  getLength () {
    return this.width * 4
   },
  constructor: createSquare
}
for(let i = 0; i < 12; i++){
  squareList[i] = createSquare(widthList[i])
  console.log(squareList[i].constructor)
}

使用new操作符號(hào)

let squareList = [];
let widthList = [5,6,5,6,5,6,5,6,5,6];
function Square(width){
  this.width = width
}
Square.prototype.getArea = function(){
  return this.width*this.width
}
Square.prototype.getLength = function(){
  return this.width*4
}
for(let i = 0; i < 12; i++){
  squareList[i] = new Square(widthList[i]);
  console.log(squareList[i].constructor)
}

總結(jié)

new X()自動(dòng)做了四件事情

  • 自動(dòng)創(chuàng)建空對(duì)象
  • 自動(dòng)為空對(duì)象關(guān)聯(lián)原型,原型地址為X.prototype
  • 自動(dòng)將空對(duì)象作為this關(guān)鍵字運(yùn)行構(gòu)造函數(shù)
  • 自動(dòng)return this
    構(gòu)造函數(shù)
  • 構(gòu)造函數(shù)本身負(fù)責(zé)給對(duì)象本身添加屬性
  • x.prototype對(duì)象負(fù)責(zé)保存對(duì)象的共用屬性

代碼規(guī)范

1 大小寫(xiě)

  • 所有構(gòu)造函數(shù)(專門(mén)用于創(chuàng)建對(duì)象的函數(shù))首字母大寫(xiě)
  • 所有被構(gòu)造出來(lái)的函數(shù)晦攒,首字母小寫(xiě)
    2 詞性
  • new 后面的函數(shù)闽撤,使用名詞形式
  • 如new Person(), new Object()
  • 其他函數(shù),一般使用動(dòng)詞開(kāi)頭
  • 如createSquare(5)脯颜、createElement('div')

如何確定一個(gè)對(duì)象的原型

為什么

  • let obj = new Object()的原型是Object.prototype
  • let arr = new Array() 的原型是Array.prototype
  • let square = new Square() 的原型 Square.prototype
  • let fn = new Function() 的原型是 Function.prototype

new操作符

  • 自動(dòng)創(chuàng)建空對(duì)象
  • 自動(dòng)為空對(duì)象關(guān)聯(lián)原型,原型地址指定為X.prototype
  • 自動(dòng)將空對(duì)象作為this關(guān)鍵字運(yùn)行構(gòu)造函數(shù)
  • 自動(dòng)return this

結(jié)論

你是誰(shuí)構(gòu)造的哟旗,你的原型就是誰(shuí)的prototype屬性對(duì)應(yīng)的對(duì)象
公式
對(duì)象.__proto__ === 構(gòu)造函數(shù).prototype 除了Object.prototype.__proto__ === null(true)

image.png

obj1 = new Object
obj1.__proto__ === Object.prototype
image.png

image.png

創(chuàng)建一個(gè)圓

function Circle(radius){
  this.radius = radius
}
Circle.prototype.getLength = function(){
  return this.radius*2*Math.PI
}
Circle.prototype.getArea = function(){
  return radius  ** 2 * Math.PI
}

對(duì)象分類

理由一

  • 有很多對(duì)象擁有一樣的行為和屬性
  • 需要把它們分為同一類
  • 如square1和square2
  • 創(chuàng)建類似的對(duì)象就會(huì)很方便

理由二

  • 但是還是有很多對(duì)象擁有其他的屬性和行為
  • 所以就需要不同的分類
  • 比如Square/Circle/React就是不同的分類
  • Array/Function也是不同的分類
  • 而Object創(chuàng)建出來(lái)的對(duì)象,是最沒(méi)有特點(diǎn)的對(duì)象

類和類型

類型

  • 類型是JS數(shù)據(jù)的分類栋操,有7種
  • Number,String,Bool,Symbol,Null, Undefined, Object

  • 類是針對(duì)于對(duì)象的分類闸餐,有無(wú)數(shù)種
  • 常見(jiàn)有Array、Function讼庇、Date等

數(shù)組對(duì)象

定義了一個(gè)數(shù)組

  • let arr = [1,2,3]
  • let arr = new Array(2,3)//定義一個(gè)有2绎巨,3兩個(gè)元素的數(shù)組
  • let arr = new Array(3)//定義一個(gè)長(zhǎng)度為三的數(shù)組


    image.png

    當(dāng)以Array(3)會(huì)定義一個(gè)長(zhǎng)度為三的空數(shù)組,但是不會(huì)初始化值蠕啄。

let arr = new Array(1,2,3);
arr.push(4);
console.log(arr);

函數(shù)

定義一個(gè)函數(shù)

  • function fn(x,y){return x+y};
  • let fn2 = function fn(x, y){return x+y};
  • let fn = (x, y) => x + y
  • let fn = new Function('x', 'y', 'return x + y')

函數(shù)對(duì)象自身屬性

  • 'name'/'length'

函數(shù)對(duì)象公有屬性

  • 'call'/'apply'/'bind'

window

構(gòu)造

  • Window
  • 可以通過(guò)constructor屬性看出構(gòu)造者

window.Object 是誰(shuí)構(gòu)造出來(lái)的

  • window.Function
  • 因?yàn)樗泻瘮?shù)都是window.Function構(gòu)造出來(lái)的

window.Function 是誰(shuí)構(gòu)造出來(lái)的

  • window.Function
  • 所有函數(shù)都是window.Function構(gòu)造出來(lái)的

window.Function 是誰(shuí)構(gòu)造出來(lái)的

  • window.Function
  • 所有都是window.Function構(gòu)造出來(lái)的
  • 瀏覽器構(gòu)造了Function场勤,然后指定了他的構(gòu)造者是自己

class

class Square{
  width = 0//初始化
  constructor(width){
    this.width = width
  }
  getArea(){
    return this.widh * this.width
  }
  getLength(){
    return this.width*4
  }
  get area2(){// 只讀屬性
    return this.width* this.width
  }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市歼跟,隨后出現(xiàn)的幾起案子和媳,更是在濱河造成了極大的恐慌,老刑警劉巖哈街,帶你破解...
    沈念sama閱讀 219,188評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件留瞳,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡骚秦,警方通過(guò)查閱死者的電腦和手機(jī)她倘,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,464評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)璧微,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人硬梁,你說(shuō)我怎么就攤上這事前硫。” “怎么了荧止?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,562評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵屹电,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我跃巡,道長(zhǎng)危号,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,893評(píng)論 1 295
  • 正文 為了忘掉前任素邪,我火速辦了婚禮外莲,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘娘香。我一直安慰自己苍狰,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,917評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布淋昭。 她就那樣靜靜地躺著,像睡著了一般安接。 火紅的嫁衣襯著肌膚如雪翔忽。 梳的紋絲不亂的頭發(fā)上盏檐,一...
    開(kāi)封第一講書(shū)人閱讀 51,708評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音龙巨,去河邊找鬼。 笑死熊响,一個(gè)胖子當(dāng)著我的面吹牛旨别,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播汗茄,決...
    沈念sama閱讀 40,430評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼秸弛,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起递览,我...
    開(kāi)封第一講書(shū)人閱讀 39,342評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤叼屠,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后非迹,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體环鲤,經(jīng)...
    沈念sama閱讀 45,801評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,976評(píng)論 3 337
  • 正文 我和宋清朗相戀三年憎兽,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片吵冒。...
    茶點(diǎn)故事閱讀 40,115評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡纯命,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出痹栖,到底是詐尸還是另有隱情亿汞,我是刑警寧澤,帶...
    沈念sama閱讀 35,804評(píng)論 5 346
  • 正文 年R本政府宣布揪阿,位于F島的核電站疗我,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏牡彻。R本人自食惡果不足惜践宴,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,458評(píng)論 3 331
  • 文/蒙蒙 一诗充、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧麦牺,春花似錦、人聲如沸鞭缭。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,008評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)岭辣。三九已至吱晒,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間沦童,已是汗流浹背仑濒。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,135評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留搞动,地道東北人躏精。 一個(gè)月前我還...
    沈念sama閱讀 48,365評(píng)論 3 373
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像鹦肿,于是被迫代替她去往敵國(guó)和親矗烛。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,055評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容