欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

簡(jiǎn)單談?wù)刯avascript高級(jí)特性

 更新時(shí)間:2019年09月04日 08:29:44   作者:雨碎江南  
看過(guò)很多關(guān)于js高級(jí)特性介紹的文章,本文是個(gè)人感覺(jué)最通俗易懂的,這里分享給大家,希望大家能夠喜歡

js中沒(méi)有class的概念,我們可以使用function來(lái)模擬。

惰性載入函數(shù)

例如我們通常使用以下的js代碼創(chuàng)建ajax:

function createXHR () {
 var xhr = null;
 try{
  xhr = new XMLHttpRequest(); // FF、Opera、Safari、IE7
 } catch(e) {
  handlerError(e);
  try{
   xhr = new ActiveXObject('Msxml2.XMLHTTP');
  } catch (e) {
   try{
    xhr = ActiveXObject('Microsoft.XMLHTTP'); 
   } catch(e) {
    xhr = null; 
   }
  }
  
 }
 return xhr;
}

function handlerError (err) {
 var errXHR = err;
 // ...
}

在現(xiàn)代的網(wǎng)絡(luò)技術(shù)中ajax技術(shù)早已是爛大街了,一個(gè)網(wǎng)頁(yè)通常包含很多的ajax——也就導(dǎo)致了頻繁創(chuàng)建xhr從而導(dǎo)致內(nèi)存泄露。我們可以采用惰性載入函數(shù)來(lái)動(dòng)態(tài)生成xhr。

/* 惰性函數(shù)(第二次生效) */
function createXHR () {
 var xhr = null;
 if (typeof XMLHttpRequest != 'undefined') {
  xhr = new XMLHttpRequest();
  createXHR = function () {
   return new XMLHttpRequest();
  }
 } else {
  try{
   xhr = new ActiveXObject('Msxml2.XMLHTTP');
   createXHR = function () {
    return new ActiveXObject('Msxml2.XMLHTTP');
   }
  } catch (e){
   try{
    xhr = new ActiveXObject('Microsoft.XMLHTTP');
    createXHR = function () {
     return new ActiveXObject('Microsoft.XMLHTTP');
    }
   } catch (e){
    createXHR = function () {
     return null;
    }
   }
  }
 }
 return xhr;
}

我們調(diào)用以上的方法創(chuàng)建xhr,在運(yùn)行第二次的時(shí)候就不用每次都判斷了,直接返回xhr。

函數(shù)柯里化

函數(shù)的柯里化(curry)把接受多個(gè)參數(shù)的函數(shù)變換成接受一個(gè)單一參數(shù)(最初函數(shù)的第一個(gè)參數(shù))的函數(shù),并且返回接受余下的參數(shù)而且返回結(jié)果的新函數(shù)的技術(shù)。,簡(jiǎn)而言之就是兩個(gè)函數(shù)的參數(shù)的合并。例如:

function curry(fn) {
 var args = Array.prototype.slice.call(arguments,1);   // 取curry的參數(shù)并將其變?yōu)閿?shù)組[100]
 console.log(args);           // [100]
 return function () {
  var innerArgs = Array.prototype.slice.call(arguments); // 匿名函數(shù)的參數(shù)列表[1,2]
  console.log(innerArgs);         // [1,2]
  var finalArgs = args.concat(innerArgs);     // 合并數(shù)組(參數(shù)合并)
  console.log(finalArgs);         // [100,1,2]  
  return fn.apply(null, finalArgs);
 }
}

function add(num1,num2,num3) {
 return num1 + num2 + num3;
}

var t = curry(add,100)(1,2);
console.info(t);

級(jí)聯(lián)函數(shù)

級(jí)聯(lián)就是一個(gè)對(duì)象將其所有有關(guān)的東西連接到了一起。如:以下的對(duì)象。

// 人:手、腿、嘴
function classPerson(){
 this.hand = "";
 this.foot = "";
 this.leg = "";
}

classPerson.prototype = {
 setHand:function(){
  this.hand = '大手';
 },
 setLeg:function () {
  this.leg = '長(zhǎng)腿歐巴';
 },
 setMouse:function () {
  this.mouse = '櫻桃小嘴';
 }
}

var person = new classPerson();
person.setHand();
person.setMouse();
person.setLeg();
console.log(person);

我們知道造人是一個(gè)整體(不可能先造手、再造腿、最后造嘴),我們現(xiàn)在的需求是一旦實(shí)例化人這個(gè)對(duì)象,該有的都有了。

簡(jiǎn)單修改以上代碼:

function classPerson(){
 this.hand = "";
 this.foot = "";
 this.leg = "";
}

classPerson.prototype = {
 setHand:function(){
  this.hand = '大手';
  return this;
 },
 setLeg:function () {
  this.leg = '長(zhǎng)腿歐巴';
  return this;
 },
 setMouse:function () {
  this.mouse = '櫻桃小嘴';
  return this;
 }
}

var person = new classPerson();
person.setHand().setMouse().setLeg(); // 調(diào)用函數(shù)
console.log(person);

我們?cè)诿總€(gè)setter中添加了return this將原有對(duì)象返回(避免無(wú)返回值的函數(shù)執(zhí)行完之后是undefined)。我們可以驚奇的發(fā)現(xiàn)常用的JQuery的鏈?zhǔn)秸{(diào)用就是一種級(jí)聯(lián)調(diào)用:
$(‘#id').show().hide().show().hide().show().hide();

javascript中的正則表達(dá)式

一般來(lái)說(shuō)//在js中表示的是單行注釋,但是一旦我們向斜杠中間加入了內(nèi)容,例如:/TEST/它就神奇地變成了正則。

模式串的聲明

var patt1 = new RegExp('hello'); // 方式一
var patt2 = /word/;    // 方式二 

test方法

我們得到了模式串(模式對(duì)象)后可以調(diào)用該方法匹配字符串,返回指定值(true or false)。

var patt = /my/;
var str = 'this is my code...';
console.log(patt.test(str)); // true

exec方法

類似于字符串的match方法,但是當(dāng)模式串指定為全局的時(shí)候兩者表現(xiàn)不同,沒(méi)有匹配返回null。

/hello/.exec('oh hello world'); // ["hello"]

以上兩個(gè)方法都是字符串對(duì)象本身的方法,下面的幾個(gè)方法是字符串中的有關(guān)正則的方法。

match方法

模式匹配。函數(shù)原型是str.mattch(pattern),將匹配的結(jié)果以數(shù)組返回。

console.log('test 123'.match(/test/g)); // [test]

replace方法

字符串替換,注意:該方法生成一個(gè)新的臨時(shí)字符串副本。如圖所示:

split方法

將字符串拆分成按照模式拆分成數(shù)組。

console.log('Hello world,hello everyone'.split(/\s+/)); // ["Hello", "world,hello", "everyone"]
console.log('Hello world,hello everyone'.split(' ')); // 等效于上面的方法

正則的類型

/pattern/attributes

attributes是可選字符串,常用的屬性是“g”(全局匹配)、“i”(大小寫不敏感)和"m"(多行匹配)

console.log('Hello world,hello everyone'.match(/hEllO/gi)); // 全局查找hello并忽略大小寫 ["Hello", "hello"]

在實(shí)際的開(kāi)發(fā)中我們可以借助在線正則調(diào)試工具來(lái)調(diào)試我們的正則,其實(shí)里面內(nèi)置了大量常用的正則。如果我們?cè)陂_(kāi)發(fā)中分析不出別人的正則的含義可以借助正則分析網(wǎng)站,將會(huì)以有限自動(dòng)機(jī)圖解的方式顯示。

正則中的元字符

正則中的元字符必須進(jìn)行轉(zhuǎn)義處理:

( [ { ^ $ | ) ? * + .]}

需要注意的是正則有2種創(chuàng)建形式:字符串字面量和new RegExp()的方式.由于RegExp的構(gòu)造函數(shù)是字符串,所以某些情況下需要進(jìn)行雙重轉(zhuǎn)義.

__PROTO__

__proto__使得繼承變得更加容易:

function Super(){};
function Sub(){};
Sub.prototype.__proto__ = Super.prototype;

這是一個(gè)非常有用的特性,可以免去如下的工作:

借助中間構(gòu)造器

無(wú)需引入第三方模塊來(lái)進(jìn)行基于原型繼承的聲明

訪問(wèn)器

可以調(diào)用方法來(lái)定義屬性,如其名有:__defineGetter__、__defineSetter__。例如為Date對(duì)象定義一個(gè)ago的屬性,返回以自然語(yǔ)言描述的日期間隔(例如:某件事發(fā)生在3秒之前)。例如:

Date.prototype.__defineGetter__('ago',function(){
 var diff = ((Date.now() - this.getTime()) / 1000)
 day_diff = Math.floor(diff / 86400)

 return day_diff == 0 && (diff < 60 && 'just now' )
  || diff < 120 && '1 minute ago' 
  || diff < 3600 && Math.floor(diff / 60) + 'minutes ago'
  || diff < 7200 && '1 hour ago'
  || diff < 86400 && Math.floor(diff / 3600) + 'hours ago'
  || day_diff == 1 && 'Yesterday'
  || diff < 7 && day_diff + ' days ago'
  || Math.ceil(day_diff / 7) + ' weeks ago'
})

var d = new Date('12/12/1990')
console.log(d.ago)

相關(guān)文章

最新評(píng)論