日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

一般人不清楚的JavaScript概念

發(fā)布時(shí)間:2023/12/15 javascript 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 一般人不清楚的JavaScript概念 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

注:以下部分代碼或術(shù)語(yǔ)涉及ECMAScript 6。

1. literal

literal(字面量)即開發(fā)者寫的,可直接被解析器識(shí)別,不必顯式依賴API創(chuàng)建的量。如:

  • 數(shù)字literal:123, 0b1111011, 0o173, 0x7B
  • 字符串literal:"123", `12{2+1}`
  • 數(shù)組literal:[1,2,8]
  • 對(duì)象literal:{A:1,B:2,C:8}
  • 正則表達(dá)式literal:/^\d+(?:\.\d+)?$/
  • 其他literal:true, false, null

2. IIFE

還記得為了閉包(closure)而編寫的聲明即執(zhí)行的函數(shù)表達(dá)式嗎?這種函數(shù)表達(dá)式是IIFE,讀作[iffy]。

var chars=//charCode為0x20,0x21,...,0x7F的一組字符組成的字符串,由以下IIFE表示 (function(){var a=[];for(let i=0x20;i<0x80;i++){a.push(i);}return String.fromCharCode.apply(String,a); })();

3. property and expando

property對(duì)于一般JavaScript開發(fā)者來(lái)說(shuō),熟悉而又陌生:
一個(gè)object的property含property key和property descriptor兩部分。
property key分string和symbol兩種。
property descriptor分data descriptor和accessor descriptor兩種。
data descriptor有固定的value,而accessor descriptor有g(shù)etter與/或setter。
property descriptor中的enumerable,configurable分別表示property是否可枚舉,是否可重定義。

expando是property的一個(gè)形容詞,指在規(guī)范之外擴(kuò)展的,僅供實(shí)現(xiàn)者內(nèi)部使用的。
對(duì)于jQuery而言,jQuery.fn屬性可算作一個(gè)expando property。

舉例:

var lengthExpando=Symbol.for("length"); function ArrayLike(){var length=0;// ...Object.defineProperty(this,lengthExpando,// symbol as a property key{enumerable:false,configurable:true,value:length}// data descriptor);// define an expando propertyObject.defineProperty(this,"length",// string as a property key{enumerable:false,configurable:true,get:function(){return this[lengthExpando];},set:function(value){var num=+value;var len=num>>>0;if(len!=num){throw new RangeError("Invalid length");}// ...this[lengthExpando]=len;}}// accessor descriptor);// define a spec-defined property }

4. mixin

與plugin可被安裝到兼容的宿主程序的概念類似,mixin是一個(gè)class/object,它的屬性/方法可動(dòng)態(tài)復(fù)制到其他適配的class/object。
舉例:

// define a mixin which is compatible with any Array-like class or object var ArrayLikePrototype={get firstElement(){var index=0;if(index<this.length){return this[index];}//return undefined;},getFirstElement(){var index=0;if(index<this.length){return this[index];}//return undefined;},get lastElement(){var index=this.length-1;if(index>-1){return this[index];}//return undefined;},getLastElement(){var index=this.length-1;if(index>-1){return this[index];}//return undefined;},// ... };// mix `ArrayLikePrototype` in `NodeList.prototype` Object.defineProperties(NodeList.prototype,Object.getOwnPropertyDescriptors(ArrayLikePrototype));// mix `ArrayLikePrototype` in `HTMLCollection.prototype` Object.defineProperties(HTMLCollection.prototype,Object.getOwnPropertyDescriptors(ArrayLikePrototype)); //so that `document.children.firstElement` will be document.children[0]// or even mix `ArrayLikePrototype` in `Array.prototype` Object.defineProperties(Array.prototype,Object.getOwnPropertyDescriptors(ArrayLikePrototype)); //so that `[1,2,3].lastElement` will be 3, and `[1,2,3].getLastElement()` will be 3 too

5. shim and polyfill

shim 是為修正運(yùn)行環(huán)境API而編寫的代碼。如:

// shim Window#setTimeout() if(document.documentMode==9&&!Object.prototype.hasOwnProperty.call(window,"setTimeout")){var WindowSetTimeout=Window.prototype.setTimeout;Window.prototype.setTimeout=function setTimeout(callback, delay){if(arguments.length<3||typeof callback!="function")return WindowSetTimeout.call(this, callback, delay);var args=Array.prototype.slice.call(arguments, 2);return WindowSetTimeout.call(this, function(){callback.apply(this, args);}, delay);}; }//shim window.Event if(document.documentMode>8&&typeof window.Event=="object"){var nativeEvent=window.Event;var shimedEvent=function Event(type/*,eventInit*/){if(!(this instanceof Event))throw new TypeError("Failed to construct 'Event': Please use the 'new' operator, this DOM object constructor cannot be called as a function.");if(arguments.length===0)throw new TypeError("Failed to construct 'Event': An event type must be provided.");var event=document.createEvent("Event"),eventInit={bubbles:false,cancelable:false},p=Object.assign(eventInit,arguments[1]);event.initEvent(type,p.bubbles,p.cancelable);return event;};shimedEvent.prototype=nativeEvent.prototype;window.Event=shimedEvent; }

polyfill是為填補(bǔ)運(yùn)行環(huán)境缺失的功能而提供的變通實(shí)現(xiàn)代碼。如:

// polyfill Object.assign() if(typeof Object.assign!="function"){Object.assign=function assign(object,source){var to,from,sources,nextSource,i,j,len,keys,key;//#1if(object==null)throw new TypeError("Cannot convert "+object+" to Object");to=Object(object);//#3if(arguments.length===1)return to;//#4sources=Array.prototype.slice.call(arguments,1);//#5for(i=0; i<sources.length; i++){//#anextSource=sources[i];if(nextSource==null){continue;}//#bfrom=Object(nextSource);//#dkeys=Object.keys(from);//#elen=keys.length;//#ffor(j=0;j<len;j++){key=keys[j];to[key]=from[key];}}//#6return to;}; } // polyfill HTMLElement#hidden if(typeof document.documentElement.hidden!="boolean"){Object.defineProperty(HTMLElement.prototype,"hidden",{get:function getHidden(){return this.getAttribute("hidden")!=null;},set:function setHidden(v){if(v)this.setAttribute("hidden", "");elsethis.removeAttribute("hidden");}}); }

6. SemVer

越扯越遠(yuǎn),這一條似乎與JavaScript不相關(guān)。

平時(shí)我們看到的一些軟件庫(kù)文件名如jquery-2.1.3.js,知道其中的版本號(hào)2.1.3遵循X.Y.Z,但XYZ每部分的含義,升級(jí)規(guī)則和比較/排序規(guī)則卻又不清楚。
為了定義一個(gè)通用的版本號(hào)標(biāo)準(zhǔn),GitHub聯(lián)合創(chuàng)始人Tom Preston-Werner編寫了SemVer(Semantic Versioning)規(guī)范。
SemVer 2.0.0規(guī)范定義的版本號(hào)的格式如下:
major . minor . patch [ - pre] [ + build]
一個(gè)SemVer版本號(hào)不僅僅代表著一個(gè)獨(dú)特的版本,還牽涉到其背后一系列的排序/升級(jí)/關(guān)聯(lián)/篩選規(guī)則。

7. vanilla

這里的vanilla指尋常的,無(wú)特色的。vanilla js用中文說(shuō)即純手工編寫,未用到第三方框架的js。

VanillaJS是一個(gè)沒(méi)有可運(yùn)行代碼的JavaScript框架,通常被用作愚人節(jié)玩笑。它列舉一些直接調(diào)用原生API而不必借助框架的實(shí)踐方式,并突出這些尋常代碼相對(duì)基于框架調(diào)用的代碼所帶來(lái)的性能優(yōu)勢(shì),其思想有種道家無(wú)為的意味。但恰恰其他框架在試圖證明使用框架后相對(duì)VanillaJS所帶來(lái)的兼容/易用優(yōu)勢(shì)。

to be continued

更多見(jiàn) MDN詞匯表

總結(jié)

以上是生活随笔為你收集整理的一般人不清楚的JavaScript概念的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。