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

為了賬號安全,請及時綁定郵箱和手機立即綁定

JavaScript tips and tricks

標簽:
JavaScript

Produce boolean value from other types
All objects in JavaScript can be converted to boolean implicitly, look at these examples:

0 == false; // true
1 == true; // true
'' == false // true
null == false // true

but these values are not the type of boolean.
Therefore if we compare empty string with false, the result is false:

0 === false; // false
1 === true; // false
'' === false // false
null === false // false

Now, the problem is how to generate boolean values from other types:

!!0 === false; // true
!!1 === true; // true
!!'' === false // true
!!null === false // true

pretty cool!

Initialize default value
JavaScript doesn’t have the overload feature of other object-oriented languages.
But the arguments of JavaScript function are optional, missed arguments are passed in with the value of undefined:

function plus(base, added) {
    return base + added;
}
plus(2); // NaN

In this example, plus(2) is the same as plus(2, undefined), the result of 2 + undefined is NaN.

Now the question is how to set the default value of the argument added:

function plus(base, added) {
    added = added || 1;
    return base + added;
}
plus(2); // 3
plus(2, 2); // 4

Prevent your page from loading in frames
If your site become famous, other people or feed aggregator may embed your page in there site with their interface. This most likely to steal data.

Now it’s time to prevent this action:

if(top !== window) {
	top.location.href = window.location.href;
}

This piece of JavaScript code should be put in the head section of all your pages.

Replace string
The String.prototype.replace method always suprise somebody who are familar with C# or Java.
Consider this code for example:

'Hello world, hello world'.replace('world', 'JavaScript');
// The result is "Hello JavaScript, hello world"

The first parameter of replace is a Regular Expression.
If it’s presented as a string, only the first found string will be replaced.

To solve this problem, we must use Regular Expression in JavaScript like this:

'Hello world, hello world'.replace(/world/g, 'JavaScript');
// The result is "Hello JavaScript, hello JavaScript"

Also, we can specify to ignore case when replace string:

'Hello world, hello world'.replace(/hello/gi, 'Hi');
// The result is "Hi world, Hi world"

Transform the arguments object into an array
Arguments variable that exist in function is a built-in, array-like object.
It’s weird that the arguments has length properties, but doen’t support slice, push, sort, etc.

Now it’s the show time to convert this fake array to a real array:

function args() {return [].slice.call(arguments, 0);}args(2, 5, 8); 
// [2, 5, 8]
點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

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

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

100積分直接送

付費專欄免費學

大額優(yōu)惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消