CoursesWeb
on 2012-04-30 08:14:11
Hi,
To get and also check the type of a variable, I use this:
function gettype(obj, type) {
// www.coursesweb.net/javascript/
// if type not specified (null), returns a string with the object (obj) type
if(type == null) return obj.constructor.toString().split(' ')[1].replace(/\(\)/g,'').toLowerCase();
else {
//returns true if is it is type, else, false
if (obj.constructor.toString().match(new RegExp(type, 'i', 'g'))) return true;
else return false;
}
}
- If the type of "obj" is the name specified in the "type" parameter, the function returns True, otherwise, returns False.
- If no argument specified for "type", the function returns a string with the type of "obj"
Brett Zamir
on 2009-04-30 06:17:56
@KELAN, While it is a nice try and would probably work in most cases, I agree with Kevin that we should avoid relying on the default toString() implementation. See the discussion for is_array() for possible solutions to the perennial challenge with arrays. :) However, I do agree with your desire to allow testing for Date, RegExp and along with simplifying our implementation I've added support for these and also our own resource type: http://trac.phpjs.org/projects/phpjs/browser/trunk/functions/var/gettype.js Thanks!
Kevin van Zonneveld
on 2009-04-29 15:26:56
@ KELAN: I noticed your version relies on toString. This raises a question for me. What if people supply objects that actually return some custom string?
KELAN
on 2009-04-28 08:50:03
function gettype(mixed_var){
switch (Object.prototype.toString.apply(mixed_var)){
case '[object Array]' : return 'array';
case '[object Function]': return 'function';
case '[object String]' : return 'string';
case '[object RegExp]' : return 'regexp';
case '[object Boolean]' : return 'boolean';
case '[object Date]' : return 'date';
case '[object Math]' : return 'math';
case '[object Number]' : {
if (parseFloat(mixed_var) != parseInt(mixed_var)) return 'double';
else return 'integer';
}
case '[object Object]' : {
if(mixed_var===undefined)return 'undefined';
else if(mixed_var==null)return 'NULL';
else return 'object';
}
default : return 'unknown type';
}
}
Brett Zamir
on 2009-01-28 14:09:12
Yes, this function won't work unless you pass the variable as a string (and then only if the variable is in a global context). There's no other way to change the type of the variable by reference like that, so that was the only way we can mimic the PHP behavior here.
$foo = "5bar"; // string
$bar = true; // boolean
settype('$foo', "integer"); // $foo is now 5 (integer)
alert(typeof $foo); // number
settype('$bar', "string"); // $bar is now "1" (string)
alert(typeof $bar); // string
Kevin van Zonneveld
on 2009-01-26 01:32:43
@ Brett Zamir & Onno Marsman: Can't submit yet, but actually made some progress with the UI today:
http://phpjs.org/packages/configure
You can play around with the get_html_translation_table, html_entity_decode, htmlentities, htmlspecialchars, htmlspecialchars_decode a bit to get the idea.
I personally would like to focus my time a bit more towards the site for now. Brett, is it an idea we get you SVN access? We don't always agree on implementations but I'm sure by now you have a solid understanding beforehand of what functions are controversial in the eyes of Onno & myself. We could always talk about it then.
This way you can directly patch/add your functions, and I can use my IDE primarily for phpjs.org for the time being. If not: no problem at all, Just let me know ok? Might as well pimp you to core member if you're interested (in fact there's no real difference, just the name) :)
Kevin van Zonneveld
on 2009-01-25 15:45:18
@ Brett Zamir: For some reason when it reaches case 'integer', I can't set the this[vr] to anything anymore. Strange, because the other (var bar) example does work.
And by the way, I believe congratulations are in order Brett:
http://phpjs.org/authors/index :)
I'm going to do some work on the site now, Pedro Sland has supplied a jquery component to the compiler I'm anxious to wire up.
Kevin van Zonneveld
on 2009-01-25 15:11:10
@ Brett Zamir: Processed everything: still having a bit of a problem with the first testcase. It continues to return 5bar but I will look into it.
Brett Zamir
on 2009-01-24 12:22:20
The regular expression line under 'float' should be changed to this:
var mtch = v.match(/^([+-]?)(\d+(\.\d+)?|\.\d+)([eE][+-]?\d+)?/);
(Had forgotten the +/- after exponent...)
Brett Zamir
on 2009-01-24 11:12:13
Sorry, I see you already have is_double(), but you CAN add is_real() as an alias of is_float()...
Brett Zamir
on 2009-01-24 11:09:50
By the way, you can get doubleval() by returning floatval() on it, and get is_double() by returning is_float().
I think you should change get_resource_type() and is_resource() to not porting (unless you just want to return false for the latter!), since JavaScript doesn't have the resource type.
Brett Zamir
on 2009-01-24 11:05:22
Missing a semicolon with return in is_array...
Brett Zamir
on 2009-01-24 11:02:10
Here's settype() following PHP behavior
// Credits to Crockford also
// only works on global variables, and "vr" must be passed in as a string
function settype (vr, type) {
function is_array(arr) {
return typeof arr === 'object' && typeof arr.length === 'number' &&
!(arr.propertyIsEnumerable('length')) &&
typeof arr.splice === 'function'
}
v = this[vr];
try {
switch(type) {
case 'boolean':
if (is_array(v) && v.length === 0) {this[vr]=false;}
else if (v === '0') {this[vr]=false;}
else if (typeof v === 'object' && !is_array(v)) {
var lgth = false;
for (var i in v) {
lgth = true;
}
this[vr]=lgth;
}
else {this[vr] = !!v;}
break;
case 'integer':
if (typeof v === 'number') {this[vr]=parseInt(v, 10);}
else if (typeof v === 'string') {
var mtch = v.match(/^([+-]?)(\d+)/);
if (!mtch) {this[vr]=0;}
else {this[vr]=parseInt(v, 10);}
}
else if (v === true) {this[vr]=1;}
else if (v === false || v === null) {this[vr]=0;}
else if (is_array(v) && v.length === 0) {this[vr]=0;}
else if (typeof v === 'object') {this[vr]=1;}
break;
case 'float':
if (typeof v === 'string') {
var mtch = v.match(/^([+-]?)(\d+(\.\d+)?|\.\d+)([eE]\d+)?/);
if (!mtch) {this[vr]=0;}
else {this[vr]=parseFloat(v, 10);}
}
else if (v === true) {this[vr]=1;}
else if (v === false || v === null) {this[vr]=0;}
else if (is_array(v) && v.length === 0) {this[vr]=0;}
else if (typeof v === 'object') {this[vr]=1;}
break;
case 'string':
if (v === null || v === false) {this[vr]='';}
else if (is_array(v)) {this[vr]='Array';}
else if (typeof v === 'object') {this[vr]='Object';}
else if (v === true) {this[vr]='1';}
else {this[vr] += '';} // numbers (and functions?)
break;
case 'array':
if (v === null) {this[vr] = [];}
else if (typeof v !== 'object') {this[vr] = [v];}
break;
case 'object':
if (v === null) {this[vr]={};}
else if (is_array(v)) {
for (var i=0, obj={}; i < v.length; i++) {
obj[i] = v;
}
this[vr] = obj;
}
else if (typeof v !== 'object') {this[vr]={scalar:v};}
break;
case 'null':
delete this[vr];
break;
}
return true;
}
catch (e) {
return false;
}
}
var a =5;
settype('a', 'object');
alert(typeof a);
Onno Marsman
on 2009-01-15 20:44:04
Unless you really want to check the javascript type. I guess functions that check for the type array really do this to make a distinction between associative array like objects and real javascript arrays. So I don't really think there is a problem...
Kevin van Zonneveld
on 2009-01-14 10:38:32
@ Onno Marsman: Yes but since this was such a controversial issue, I really do like to have that check in place.
Just like we now wish that we had implemented is_array in every function before so that we get only one point of control we need to update when a decision is made.
Onno Marsman
on 2009-01-13 18:12:14
Because of the way is_array is implemented this function will never return 'object'. The call to is_object is now useless.