Check function name in Javascript
Here is my code:
object = {'name' : String}
object = {'age' : Number开发者_如何学C}
typeof object.name // 'function'
typeof object.age // 'function'
Is it possible to check that object.name is a String and object.age is a Number?
Using typeof only gives me back 'function'.
Rather than doing:
object = {'name' : String}
object = {'age' : Number}
You should check for actual data type:
object = {'name' : 'test', 'age' : 123}
And here is how you can check their type:
alert(typeof(object.name));
alert(typeof(object.age));
Output:
string
number
var object = {
"name" : "test",
"age" : 123
};
if (!isNaN(parseInt(object.age, 10))) {
// It is a numerical value (since this is an age, an int may be appropriate)
alert("It's numeric!");
}
if (object.name.toString() === object.name) {
// It is really a string
alert("It's a string!");
}
精彩评论