phpjs
Version:
61 lines (51 loc) • 1.93 kB
Markdown
layout: page
title: "JavaScript is_resource function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/is_resource:781
- /functions/view/is_resource
- /functions/view/781
- /functions/is_resource:781
- /functions/781
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's is_resource
{% codeblock var/is_resource.js lang:js https://raw.github.com/kvz/phpjs/master/functions/var/is_resource.js raw on github %}
function is_resource (handle) {
// From: http://phpjs.org/functions
// + original by: Brett Zamir (http://brett-zamir.me)
// + improved by: Luis Salazar (http://www.freaky-media.com/)
// * example 1: is_resource('a');
// * returns 1: false
var getFuncName = function (fn) {
var name = (/\W*function\s+([\w\$]+)\s*\(/).exec(fn);
if (!name) {
return '(Anonymous)';
}
return name[1];
};
return !(!handle || typeof handle !== 'object' || !handle.constructor || getFuncName(handle.constructor) !== 'PHPJS_Resource');
}
{% endcodeblock %}
- [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/var/is_resource.js)
Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are
the closest match to this hashtable-like data structure.
Please also note that php.js offers community built functions and goes by the
[McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online
functions that are far from perfect, in the hopes to spark better contributions.
Do you have one? Then please just:
- [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/var/is_resource.js)
### Example 1
This code
{% codeblock lang:js example %}
is_resource('a');
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
false
{% endcodeblock %}
### Other PHP functions in the var extension
{% render_partial _includes/custom/var.html %}