Function: get_class version 97

Function:
Description: Retrieves the class name.
Version:
Status: latest
Date: 2011-12-20 05:02
Summary: Import from phpjs.
Programmer: jj5
Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
function get_class (obj) {
    // http://jsphp.co/jsphp/fn/view/get_class
    // +   original by: Ates Goral (http://magnetiq.com)
    // +   improved by: David James
    // +   improved by: David Neilsen
    // *     example 1: get_class(new (function MyClass() {}));
    // *     returns 1: "MyClass"
    // *     example 2: get_class({});
    // *     returns 2: "Object"
    // *     example 3: get_class([]);
    // *     returns 3: false
    // *     example 4: get_class(42);
    // *     returns 4: false
    // *     example 5: get_class(window);
    // *     returns 5: false
    // *     example 6: get_class(function MyFunction() {});
    // *     returns 6: false
    if (obj && typeof obj === 'object' && 
			Object.prototype.toString.call(obj) !== '[object Array]' && 
			obj.constructor && obj !== this.window) {
        var arr = obj.constructor.toString().match(/function\s*(\w+)/);

        if (arr && arr.length === 2) {
            return arr[1];
        }
    }

    return false;
}
[top]

Comments

There are no comments yet, be the first!

Please Login or Register to post comments.