第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

Optimizing JavaScript Code - 1

標(biāo)簽:
JavaScript

There is an excellent article named
Optimizing JavaScript Code. The authors are software engineers on Gmail
and Google chrome.

I appreciate the knowledge described in this article and try to repeat it in my
own words.

Faster string concatenation

I have described this rule in my article
JavaScript Tips and Tricks - 5.

    // BAD    var longStr = "This is a very long " +        "long long " +        "long string."    // GOOD    var longStr = ["This is a very long ",        "long long ",        "long string."].join("");

Notice: the original article use join() to concat string is incorrect,
which is the same as join(”,”).

Avoid create too many temporary strings

Build up long strings by pass array into functions, to avoid create too many temporary
strings.

Remeber in my last article -
Reference and array clone, i describe how an array is passing by reference.

    // BAD    function createMenu(index) {        return "<li>Menu " + index + "</div>";    }     var arr = ["<ul>"];    for (var i = 0; i < 100; i++) {        arr.push(createMenu(i));    }    arr.push("</ul>");    var menu = arr.join("");     // GOOD    function createMenu(index, arr) {        arr.push("<li>Menu " + index + "</div>");    }     var arr = ["<ul>"];    for (var i = 0; i < 100; i++) {        createMenu(i, arr);    }    arr.push("</ul>");    var menu = arr.join("");

Define class method in prototype

    // BAD    function Person(name) {        this.name = name;        this.getName = function() {            return this.name;        };    }     // GOOD    function Person(name) {        this.name = name;    }    Person.prototype.getName = function() {        return this.name;    };

The first method is inefficient, as each time a instance of Person is constructed,
a new function and closure is created.

I also mention the prototype property in this article -
Prototype/Constructor that i have known

Don’t define reference type in prototype

I have describe this rule in my article -
Athena JavaScript Questions - 2.

You can also refer to that article to discover why we should not define reference
type in prototype.

But this google article suggest define value type variables in prototype (such as
Number, Boolean, String, null, undefined).

The purpose is to avoid unnecessarily running the initialization code each time
the constructor is called.

For example:

    // Google prefered way:    function Person(name) {        if (name) {            this.name = name;        }    }    Person.prototype.name = "Unknown";    Person.prototype.getName = function() {        return this.name;    };     var p1 = new Person();    console.log(p1.getName());  // "Unknown"     var p2 = new Person("ZhangSan");    console.log(p2.getName());  // "ZhangSan"

Notice:We don’t have to initialize the name property when the p1 is under construction.

This may speed up the initialization and save some memory storage.

But the effects is very little if the variable is not a very very long string.

Therefore, i perfer writing the previous example in this way:

    // I prefered way:    function Person(name) {        this.name = name || "Unknown";    }    Person.prototype.getName = function() {        return this.name;    };     var p1 = new Person();    console.log(p1.getName());     var p2 = new Person("ZhangSan");    console.log(p2.getName());
點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消