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

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

JavaScript tips and tricks - 3

標簽:
JavaScript

Is undefined a reserved word
It seems like so, but actually it doesn’t.

var undefined = 'Hello';   undefined; // 'Hello'

This may surprise you, but it does work well. undefined is just a pre-defined variable.

Note: Never assign a value to undefined, that may break your program.

How to check a undefined value
In two situations, a variable is identified as a variable that is not defined.
1. A variable is declared, but not assigned.

var name;   name === undefined; // true

2. A variable is never declared.

name2 === undefined; // error – name2 is not defined

You can see in the second situation an error which has been thrown out.
How to catch this error?
We can use the following way which is vary common to check whether a variable is defined or not:

typeof(name2) === ‘undefined’; // true

how to preload images
The concept of “preload images” is about loading images that are not exist in the document and displaying them later using javascript to response to user’s action. For example, you may want to change an image to another one when mouse hover.

The basic preload syntax like this:

var img = new Image();   img.src = ‘clock2.gif’;

Then you can use it as follows:

<img class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="clock.gif" mce_class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="clock.gif" alt=""   onmouseover="this.src='clock2.gif';"   onmouseout="this.src=clock.gif';" />

Then, how to preload an array of images:

var source = ['img1.gif','img2.gif'];   var img = new Image();   for(var i = 0; i < source.length; i++) {       img.src = source[i];   }

This will only preload the last image. The rest of the images have no chance to preload before the loop come around again. The right way to do would be:

var source = ['img1.gif','img2.gif'];   for(var i = 0; i < source.length; i++) {       var img = new Image();       img.src = source[i];   }

JavaScript Closures
A closure is a local variable for a function keeps alive after the function has returned.
If you use the function keyword inside another function, you are creating a closure.
A famous example:

function add(i) {       return function() {           return ++i;       };   }   add(2).toString(); // "function () { return ++i; }"   add(2)(); // 3

add(2) is a function, which can access the outer function’s local parameter i.
It’s the power of closure.

You can refer to this wonderful resource for details.

Private variable
JavaScript doesn’t have the mechanism of hidden variable from the outside world.
Sometimes we use name conventions to identify whether a variable is a private member:

var person = {       _name: '',       getName: function() {           return this._name || 'not defined';       }   };   person.getName(); // "not defined"

The underline prefix name convention is used to identify a private member.
But other people can invoke it regardless what name conventions have been applied.

person._name; // ""

Then, how to implement a real private member in javascript?
The core technology to implement a private variable is anonymous function and closure.

var person = {};   (function() {       var _name = '';       person.getName = function() {           return _name || 'not defined';       }   })();   person.getName(); // "not defined"   typeof(person._name); // "undefined"
點擊查看更多內(nèi)容
TA 點贊

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

評論

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

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

100積分直接送

付費專欄免費學

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

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

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

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

舉報

0/150
提交
取消