首頁  >  文章  >  web前端  >  jQuery.isPlainObject() 函式使用詳解

jQuery.isPlainObject() 函式使用詳解

巴扎黑
巴扎黑原創
2017-07-08 11:36:111514瀏覽

jQuery.isPlainObject()函數用來判斷指定參數是否為純粹的物件。

所謂"純粹的物件",就是該物件是透過"{}"或"new Object"建立的。

該函數屬於全域jQuery物件。

語法

jQuery 1.4 新增#靜態函數。

jQuery.isPlainObject( object )

參數

參數

##說明

object    任意類型需要進行判斷的任意值。    

注意:宿主對象(或其它被瀏覽器宿主環境使用的對象,以完成ECMAScript的執行環境)難以進行跨平台的特性偵測。因此,對於這些物件的實例,$.isPlainObject()在不同的瀏覽器上可能會得出不同的結果。

傳回值

jQuery.isPlainObject()

函數的回傳值為Boolean類型,如果指定的參數是純粹的對象,則傳回true,否則傳回false。

範例&說明

jQuery.isPlainObject()函數的jQuery範例程式碼如下:

//在当前页面内追加换行标签和指定的HTML内容
function w( html ){
    document.body.innerHTML += "<br/>" + html;
}
w( $.isPlainObject( { } ) ); // true
w( $.isPlainObject( new Object() ) ); // true
w( $.isPlainObject( { name: "CodePlayer"} ) ); // true
w( $.isPlainObject( { sayHi: function(){} } ) ); // true
w( $.isPlainObject( "CodePlayer" ) ); // false
w( $.isPlainObject( true ) ); // false
w( $.isPlainObject( 12 ) ); // false
w( $.isPlainObject( [ ] ) ); // false
w( $.isPlainObject( function(){ } ) ); // false
w( $.isPlainObject( document.location ) ); // false(在IE中返回true)
function Person(){
    this.name = "张三";
}
w( $.isPlainObject( new Person() ) ); // false

以上是jQuery.isPlainObject() 函式使用詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn