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

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

JavaScript tips and tricks - 5

標(biāo)簽:
JavaScript

Fast string concatenation
We always use + to concatenate small strings into a large one, which is considered to be a good practice.
But there will be a major hit on performance if you do many string concatenation operations on IE browser.
Consider these two comparison examples:

var startTime = new Date();var str = '';for (var i = 0; i < 50000; i++) {    str += i;}alert(new Date() - startTime);  // Firefox - 18ms, IE7 - 2060ms

var startTime = new Date();
var arr = [];
for (var i = 0; i < 100000; i++) {
  arr.push(i);
}
var str = arr.join(”);
alert(new Date() - startTime); // Firefox - 38ms, IE7 - 280ms
Firefox seems to do some optimization on + operator, while IE is like a stupid guy.

The unary + operator

In JavaScript it is possible to use the + operator alone before a single element.
This indicates a math operation and tries to convert the element to a number.
If the conversion fails, it will evaluate to NaN.

See some example:

2 + '1'; // "21"2 + ( +'1'); // 3

The unary + operator, when used on types other than string, will internally attempt to call valueOf() or toString() (in that order) and then attempt to convert the result to a number.

+new Date; // 1242616452016+new Date === new Date().getTime(); // true+new Date() === Number(new Date) // true

Refer to this article.

encodeURI() and encodeURIComponent()
The encodeURI method returns an encoded URI, which does not encode the following characters: “:”, “/”, “;”, and “?”.
The encodeURIComponent is designed to encode these characters.
Let’s see it in action:

'index.jsp?page='+encodeURI('/page/home.jsp');  // "index.jsp?page=/page/home.jsp"'index.jsp?page='+encodeURIComponent('/page/home.jsp');  // "index.jsp?page=%2Fpage%2Fhome.jsp"

The encodeURIComponent() method should be used in most case when encoding a single component of a URI.

table.innerHTML in IE
It’s a convenience to set element content via innerHTML property.
For example:

<div id="container1"> </div>document.getElementById('container1').innerHTML = "HelloWorld!";

But setting table.innerHTML causes error in IE:

<table id="table1"> </table>document.getElementById('table1').innerHTML = "<tr><td>Hello</td><td>World!</td></tr>";// works well in Firefox, but fail to work in IE

Actually, the innerHTML property of the TABLE,THEAD, TR, SELECT elements in IE are read-only.
How to build a table dynamically?

<div id="table1"> </div>document.getElementById('table1').innerHTML = "<table><tr><td>Hello</td><td>World!</td></tr></table>";

0.1+0.2 != 0.3
JavaScript treat decimals as floating point numbers, which may cause rounding errors.

0.1 + 0.2; // 0.30000000000000004

You can round it off as follows:

(0.1 + 0.2).toFixed(); // "0"

 

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

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

評(píng)論

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

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

100積分直接送

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

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

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

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

幫助反饋 APP下載

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

公眾號(hào)

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

舉報(bào)

0/150
提交
取消