JavaScript get_object_vars
Returns an array of object properties
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 | function get_object_vars (obj) { // Returns an array of object properties // // version: 1008.1718 // discuss at: http://phpjs.org/functions/get_object_vars // + original by: Brett Zamir (http://brett-zamir.me) // * example 1: function Myclass () {this.privMethod = function (){}} // * example 1: Myclass.classMethod = function () {} // * example 1: Myclass.prototype.myfunc1 = function () {return(true);}; // * example 1: Myclass.prototype.myfunc2 = function () {return(true);} // * example 1: get_object_vars('MyClass') // * returns 1: {} var retArr = {}, prop = ''; for (prop in obj) { if (typeof obj[prop] !== 'function' && prop !== 'prototype') { retArr[prop] = obj[prop]; } } for (prop in obj.prototype) { if (typeof obj.prototype[prop] !== 'function') { retArr[prop] = obj.prototype[prop]; } } return retArr; } |
Examples
Running
1
2
3
4
5 | function Myclass () {this.privMethod = function (){}} Myclass.classMethod = function () {} Myclass.prototype.myfunc1 = function () {return(true);}; Myclass.prototype.myfunc2 = function () {return(true);} get_object_vars('MyClass') |
Should return
1 | {} |
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 get_object_vars goodness in JavaScript.
No comments yet. Be the first!
spread the word:
Use any PHP function in JavaScript
These kind folks have already donated: @HalfWinter, Paulo Freitas, Andros Peña Romo, @andorosu, Raimund Szabo, Nitin Gupta, @nikosdion, Anonymous, Anonymous and Shawn Houser.
<your name here>