UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

88 lines (75 loc) 2.22 kB
--- layout: page title: "JavaScript get_class function" comments: true sharing: true footer: true alias: - /functions/view/get_class:409 - /functions/view/get_class - /functions/view/409 - /functions/get_class:409 - /functions/409 --- <!-- Generated by Rakefile:build --> A JavaScript equivalent of PHP's get_class {% codeblock classobj/get_class.js lang:js https://raw.github.com/kvz/phpjs/master/functions/classobj/get_class.js raw on github %} function get_class (obj) { // http://kevin.vanzonneveld.net // + 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; } {% endcodeblock %} - [view on github](https://github.com/kvz/phpjs/blob/master/functions/classobj/get_class.js) - [edit on github](https://github.com/kvz/phpjs/edit/master/functions/classobj/get_class.js) ### Example 1 This code {% codeblock lang:js example %} get_class(new (function MyClass() {})); {% endcodeblock %} Should return {% codeblock lang:js returns %} "MyClass" {% endcodeblock %} ### Example 2 This code {% codeblock lang:js example %} get_class({}); {% endcodeblock %} Should return {% codeblock lang:js returns %} "Object" {% endcodeblock %} ### Example 3 This code {% codeblock lang:js example %} get_class([]); {% endcodeblock %} Should return {% codeblock lang:js returns %} false {% endcodeblock %} ### Other PHP functions in the classobj extension {% render_partial _includes/custom/classobj.html %}