国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

jQuery原型屬性和原型方法詳解

2019-11-20 12:05:40
字體:
來源:轉載
供稿:網友

首先看一下在jQuery1.7.1中定義的原型屬性和方法有哪些?

首先是constructor屬性 

相信熟悉js面向對象部分的開發人員都熟悉,就是用來返回對象屬性創建的函數,舉個簡單的例子:

function Person(){};    var person=new Person();    alert(person.constructor); //function Person(){}

我們寫繼承的時候喜歡把所有的原型屬性和方法放在一個單獨的對象字面量中,這樣就會導致constructor屬性與“實際”指向不符合例如:

  function Person(){    }    Person.prototype={      say:function(msg){        alert(msg);       }    }    var person=new Person();    person.say('hello');    alert(person.constructor); //function Object(){[native code]}

這個時候的指向就會變化因為字面量對象是Object的一個實例自然constructor屬性就會執行Object為了糾正這個“錯誤”通常需要手動修改回來這就是源碼,源碼中constructor:jQuery的解釋

selector屬性

selector屬性對于使用jquey作為js庫來說沒有用處它主要是用于開發基于jquery的插件或者改造使用,該屬性會返回獲取當前的jquery對象的選擇器字符串,例如:

var obj=$('div a');console.log(obj.selector);//'div a'

jquery屬性

該屬性返回當前使用的jQuery版本

console.log($('body').jquery); //1.7.1

length屬性

該屬性返回jquery對象包含的元素個數例如:

console.log ( $('body') .length); //1

這4個屬性源碼如下:

  constructor: jQuery,  // Start with an empty selector  selector: "",  // The current version of jQuery being used  jquery: "1.7.1",  // The default length of a jQuery object is 0  length: 0,

size方法

// The number of elements contained in the matched element set  size: function() {    return this.length;  },

該方法就是返回jquery對象的length屬性兩者而言推薦使用length屬性,可以減少不必要的函數調用開銷

toArray方法

toArray: function() {    return slice.call( this, 0 );  },

把jQuery集合中所有DOM元素恢復成一個數組。

alert($('li').toArray());[<li id="foo">, <li id="bar">]

首先這里的slice方法在jQuery的構造函數里面已經被保留下來,就是Array的原型方法

// Save a reference to some core methods87 toString = Object.prototype.toString,88 hasOwn = Object.prototype.hasOwnProperty,89 push = Array.prototype.push,90 slice = Array.prototype.slice,91 trim = String.prototype.trim,92 indexOf = Array.prototype.indexOf,

通過call方法實現對象冒充,傳入參數0表示不進行截取,由于此方法會返回一個 clean array 也就是純數組這樣就實現了從jquery對象到純數組的轉變,在以后遇到其他類數組形式時也可以采用此方法進行轉換例如:

<!doctype html><html>  <head>    <meta charset='utf-8'/>    <title>jQuery源碼分析-原型屬性和方法</title>  </head>  <body>    <div>    </div>    <div></div>     </body>  <script src='jquery-1.7.1.js'></script>  <script>  var divs=document.getElementsByTagName('div');  console.log(divs); //[div, div]  alert(divs instanceof Array); //fasle  alert(Array.prototype.slice.call(divs,0) instanceof Array); //true  </script></html>

所以學習jqeury源碼除了對使用jquery有幫助之外還能學到很多js的使用技巧

get方法

// Get the Nth element in the matched element set OR// Get the whole matched element set as a clean array  get: function( num ) {    return num == null ?      // Return a 'clean' array      this.toArray() :      // Return just the object      ( num < 0 ? this[ this.length + num ] : this[ num ] );  },

此方法的作品是從jquery對象的元素數組中找到其中的某一個并且返回js原聲node元素對象而不是jquery對象,這是跟eq方法不同的地方 ,此方法接受一個參數如果參數不存則調用toArray方法返回包涵所有元素的數組,如果是大于0的數直接通過下下標的方式獲取即可如果是負數則通過與長度相加獲得我們寫某些方法需要支持正負數下標的一個很好的方法。如果寫的不是數字,或者超過當前對象所包含的元素長度會返回undefined.

以上所述就是本文的全部內容了,希望大家能夠喜歡。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 白山市| 抚远县| 固始县| 尖扎县| 东辽县| 桐庐县| 栾城县| 临武县| 万全县| 介休市| 鹤岗市| 松阳县| 绥宁县| 华容县| 遂平县| 绥宁县| 晋江市| 四会市| 玉龙| 凤山县| 乐业县| 长寿区| 龙山县| 隆化县| 巴中市| 错那县| 乌拉特前旗| 高密市| 辛集市| 和林格尔县| 哈尔滨市| 合山市| 沧源| 桃江县| 富川| 枝江市| 安国市| 龙川县| 平顶山市| 景泰县| 赤壁市|