phpjs
Version:
119 lines (105 loc) • 3.68 kB
Markdown
---
layout: page
title: "JavaScript gettype function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/gettype:422
- /functions/view/gettype
- /functions/view/422
- /functions/gettype:422
- /functions/422
---
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's gettype
{% codeblock var/gettype.js lang:js https://raw.github.com/kvz/phpjs/master/functions/var/gettype.js raw on github %}
function gettype (mixed_var) {
// From: http://phpjs.org/functions
// + original by: Paulo Freitas
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: Douglas Crockford (http://javascript.crockford.com)
// + input by: KELAN
// + improved by: Brett Zamir (http://brett-zamir.me)
// - depends on: is_float
// % note 1: 1.0 is simplified to 1 before it can be accessed by the function, this makes
// % note 1: it different from the PHP implementation. We can't fix this unfortunately.
// * example 1: gettype(1);
// * returns 1: 'integer'
// * example 2: gettype(undefined);
// * returns 2: 'undefined'
// * example 3: gettype({0: 'Kevin van Zonneveld'});
// * returns 3: 'array'
// * example 4: gettype('foo');
// * returns 4: 'string'
// * example 5: gettype({0: function () {return false;}});
// * returns 5: 'array'
var s = typeof mixed_var,
name;
var getFuncName = function (fn) {
var name = (/\W*function\s+([\w\$]+)\s*\(/).exec(fn);
if (!name) {
return '(Anonymous)';
}
return name[1];
};
if (s === 'object') {
if (mixed_var !== null) { // From: http://javascript.crockford.com/remedial.html
if (typeof mixed_var.length === 'number' && !(mixed_var.propertyIsEnumerable('length')) && typeof mixed_var.splice === 'function') {
s = 'array';
} else if (mixed_var.constructor && getFuncName(mixed_var.constructor)) {
name = getFuncName(mixed_var.constructor);
if (name === 'Date') {
s = 'date'; // not in PHP
} else if (name === 'RegExp') {
s = 'regexp'; // not in PHP
} else if (name === 'PHPJS_Resource') { // Check against our own resource constructor
s = 'resource';
}
}
} else {
s = 'null';
}
} else if (s === 'number') {
s = this.is_float(mixed_var) ? 'double' : 'integer';
}
return s;
}
{% endcodeblock %}
- [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/var/gettype.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/gettype.js)
### Example 1
This code
{% codeblock lang:js example %}
gettype(1);
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
'integer'
{% endcodeblock %}
### Example 2
This code
{% codeblock lang:js example %}
gettype(undefined);
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
'undefined'
{% endcodeblock %}
### Example 3
This code
{% codeblock lang:js example %}
gettype({0: 'Kevin van Zonneveld'});
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
'array'
{% endcodeblock %}
### Other PHP functions in the var extension
{% render_partial _includes/custom/var.html %}