phpjs
Version:
81 lines (70 loc) • 2.5 kB
Markdown
layout: page
title: "JavaScript get_declared_classes function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/get_declared_classes:412
- /functions/view/get_declared_classes
- /functions/view/412
- /functions/get_declared_classes:412
- /functions/412
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's get_declared_classes
{% codeblock classobj/get_declared_classes.js lang:js https://raw.github.com/kvz/phpjs/master/functions/classobj/get_declared_classes.js raw on github %}
function get_declared_classes () {
// http://kevin.vanzonneveld.net
// + original by: Brett Zamir (http://brett-zamir.me)
// + depends on: class_exists
// * example 1: function A (z) {this.z=z} // Assign 'this' in constructor, making it class-like
// * example 1: function B () {}
// * example 1: B.c = function () {}; // Add a static method, making it class-like
// * example 1: function C () {}
// * example 1: C.prototype.z = function () {}; // Add to prototype, making it behave as a "class"
// * example 1: get_declared_classes()
// * returns 1: [C, B, A]
var i = '',
j = '',
arr = [],
already = {};
for (i in this.window) {
try {
if (typeof this.window[i] === 'function') {
if (!already[i] && this.class_exists(i)) {
already[i] = 1;
arr.push(i);
}
} else if (typeof this.window[i] === 'object') {
for (j in this.window[i]) {
if (typeof this.window[j] === 'function' && this.window[j] && !already[j] && this.class_exists(j)) {
already[j] = 1;
arr.push(j);
}
}
}
} catch (e) {
}
}
return arr;
}
{% endcodeblock %}
- [view on github](https://github.com/kvz/phpjs/blob/master/functions/classobj/get_declared_classes.js)
- [edit on github](https://github.com/kvz/phpjs/edit/master/functions/classobj/get_declared_classes.js)
### Example 1
This code
{% codeblock lang:js example %}
function A (z) {this.z=z} // Assign 'this' in constructor, making it class-like
function B () {}
B.c = function () {}; // Add a static method, making it class-like
function C () {}
C.prototype.z = function () {}; // Add to prototype, making it behave as a "class"
get_declared_classes()
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
[C, B, A]
{% endcodeblock %}
### Other PHP functions in the classobj extension
{% render_partial _includes/custom/classobj.html %}