English 中文(简体)
我怎么能确定一个物体是 Java文中的“cus子”?
原标题:How can I determine that an object is "custom" in Javascript?

为了澄清我的头衔,我需要一种方法,确定物体不是“Sting”、“号码”、“Boolean”或“预先界定的Java”字标。 一种思维方式是:

if(!typeof myCustomObj == "string" && !typeof myCustomObj  == "number" && !typeof myCustomObj == "boolean") {

我可以检查一下,myCustomObj是否为物体,如:

if(typeof myCustomObj == "object") {

这只是用于原始价值,但正如这一“新强权”(“hello World”)=“object”/代码”一样。

确定某一物体是否为而不是预先界定的Java 标语,是哪些可靠途径?

最佳回答

http://api.jquery.com/jery.isPlainObject/“rel=“nofollow”

function (obj) {
    // Must be an Object.
    // Because of IE, we also have to check the presence of the constructor property.
    // Make sure that DOM nodes and window objects don t pass through, as well
    if (!obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow(obj)) {
        return false;
    }

    try {
        // Not own constructor property must be Object
        if (obj.constructor && !hasOwn.call(obj, "constructor") && !hasOwn.call(obj.constructor.prototype, "isPrototypeOf")) {
            return false;
        }
    } catch(e) {
        // IE8,9 Will throw exceptions on certain host objects #9897
        return false;
    }

    // Own properties are enumerated firstly, so to speed up,
    // if last one is own, then all properties are own.
    var key;
    for (key in obj) {}

    return key === undefined || hasOwn.call(obj, key);
}
问题回答

You can use the "toString" functon on the Object prototype:

var typ = Object.prototype.toString.call( someTestObject );

给出的答案是“[目标][目标日期]”或“[目标日期]”。 不幸的是,你可以把这种方式区分成单纯的物体和与建筑商做的事物,但从某种意义上来说,这些事情实际上没有什么不同。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签