Use PHP functions in JavaScript

JavaScript is_object

Returns true if variable is an object

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
20
function is_object (mixed_var){
    // Returns true if variable is an object  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/is_object    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Legaev Andrey
    // +   improved by: Michael White (http://getsprink.com)
    // *     example 1: is_object('23');
    // *     returns 1: false    // *     example 2: is_object({foo: 'bar'});
    // *     returns 2: true
    // *     example 3: is_object(null);
    // *     returns 3: false
    if (mixed_var instanceof Array) {        return false;
    } else {
        return (mixed_var !== null) && (typeof( mixed_var ) == 'object');
    }
}
external links: original PHP docs | raw js source

Examples

» Example 1

Running

1
is_object('23');

Should return

1
false

» Example 2

Running

1
is_object({foo: 'bar'});

Should return

1
true

Dependencies

No dependencies, you can use this function standalone.

Open syntax issues

php.js uses JsLint to help us keep our code consistent and prevent some common bugs.

Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.


Authors

Thanks to the following developers, you get to have is_object goodness in JavaScript.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
Kevin van Zonneveld
3 Mar '08 Permalink

q  @ Michael White: Good point, added!

Gravatar
Michael White
3 Mar '08 Permalink

q   Why do I make things more complicated than they really are? Here is a much more sensible version that based on the content of the is_array() method. I apologize for polluting your message boards with the previous post.

1
2
3
4
56
7
function is_object( mixed_var ){
        if(mixed_var instanceof Array) {
                return false;
        } else {
                return (mixed_var !== null) && (typeof( mixed_var ) == 'object');        }
}


http://crestidg.com

Gravatar
Michael White
3 Mar '08 Permalink

q   I found a way to detect the difference between objects and arrays. There is an incredibly slight change that you pass an array to this function and it still returns true but I'll explain that after the code.

1
2
3
4
56
7
8
9
10
alert("Obj: " + is_object({foo: "bar"}));
alert("Arr: " + is_object(["foo", "bar"]));
 
function is_object( mixed_var ){
        if(typeof(mixed_var.join) != "function") {            return (mixed_var !== null) && (typeof( mixed_var ) == 'object');
        } else {
                return false;
        }
}


Ok, this works by checking to see if there is a property of the array named "join" that contains a function object. If not then we know this is an object. If it is then we can reasonably assume that it is an array. I chose the join() method of arrays simply because it has been implemented since Netscape 3.0 and IE 3.0 and so should exist if you are working with an array.

http://crestidg.com

Gravatar
Kevin van Zonneveld
1 Mar '08 Permalink

q  @ Legaev Andrey: updated, thanks again Legaev!

Gravatar
Legaev Andrey
1 Mar '08 Permalink

q   Hi
I found few errors:
1. typeof null returns 'object', but this is not an object.
2. Original PHP function returns false if you pass array as parameter, but in JS returns 'object'. If we want fully emulate PHP function we should handle this case.

1
2
3
function is_object( mixed_var ){
    return (mixed_var !== null) && (typeof( mixed_var ) == 'object');
}


Contribute a New function