Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
352 views
in Technique[技术] by (71.8m points)

javascript - 如何在JavaScript / jQuery中获取对象的属性?(How to get an object's properties in JavaScript / jQuery?)

In JavaScript / jQuery, if I alert some object, I get either [object] or [object Object](在JavaScript / jQuery中,如果我alert一些对象,我得到[object][object Object])

Is there any way to know:(有什么办法可以知道:) what is the difference between these two objects(这两个对象有什么区别) what type of Object is this(这是什么类型的对象) what all properties does this object contains and values of each property(此对象包含的所有属性以及每个属性的值) ?(?)   ask by Saiful translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can look up an object's keys and values by either invoking JavaScript's native for in loop:(您可以通过调用JavaScript的本机for in循环来查找对象的键和值:)

var obj = { foo: 'bar', base: 'ball' }; for(var key in obj) { alert('key: ' + key + ' ' + 'value: ' + obj[key]); } or using jQuery's .each() method:(或者使用jQuery的.each()方法:) $.each(obj, function(key, element) { alert('key: ' + key + ' ' + 'value: ' + element); }); With the exception of six primitive types , everything in ECMA-/JavaScript is an object.(除六种原始类型外 ,ECMA- / JavaScript中的所有内容都是一个对象。) Arrays;(阵列;) functions;(功能;) everything is an object.(一切都是对象。) Even most of those primitives are actually also objects with a limited selection of methods.(即使大多数这些原语实际上也是具有有限选择方法的对象。) They are cast into objects under the hood, when required.(必要时,它们被铸在引擎盖下的物体中。) To know the base class name, you may invoke the Object.prototype.toString method on an object, like this:(要知道基类名称,可以在对象上调用Object.prototype.toString方法,如下所示:) alert(Object.prototype.toString.call([])); The above will output [object Array] .(以上将输出[object Array] 。) There are several other class names, like [object Object] , [object Function] , [object Date] , [object String] , [object Number] , [object Array] , and [object Regex] .(还有其他几个类名,如[object Object][object Function][object Date][object String][object Number][object Array][object Regex] 。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...