Home  >  Article  >  Web Front-end  >  JavaScript type checking and internal properties [[Class]]

JavaScript type checking and internal properties [[Class]]

黄舟
黄舟Original
2017-02-28 14:53:141250browse

All objects contain an internal property [[Class]]

We cannot access this property directly, but we can access it through a method
This method is Object.prototype.toString.call(...)
This is also a more reliable way to check the type (instanceof, constructor, etc. are not reliable)

For example, we detect an array

Object.prototype.toString.call([1,2,3]);

Console printing"[object Array]"
The "Array" here actually calls the [[Class]] attribute inside the native function Array()
We can encapsulate a function to detect the type

function classOf(obj){
    return Object.prototype.toString.call(obj).slice(8,-1);
}

The function is very simple. It simply calls the toString method on the Object prototype and intercepts the information that is useful to us
We can try this function

console.log(classOf({'a':1}));
console.log(classOf([1,2,3]));
console.log(classOf(function a(){}));
console.log(classOf(/a/));
console.log(classOf(123));
console.log(classOf('abc'));
console.log(classOf(true));
console.log(classOf(Symbol()));
console.log(classOf(undefined));
console.log(classOf(null));

Console printing

But we found that not only objects, but even basic type values ​​string, number, and boolean can be printed
This is because JavaScript is the basic The type value wraps an encapsulated object
, turning them into objects, and String(), Number(), and Boolean() have attributes [[Class]]
Although undefined and null have no corresponding native functions here Undefined() and Null(), but there are still internal properties returned

Through this function we can accurately judge the type
We can even judge the parameter list

function demo(){
    console.log(classOf(arguments));
}
demo();//"Arguments"

Back Object.prototype.toString.call(...) Why does it have to go through such a troublesome string to determine the type?
We can see what happens if we call toString() directly

console.log({'a':1}.toString());
console.log([1,2,3].toString());
console.log(function a(){}.toString());
console.log(/a/.toString());
console.log(123..toString());
// 只写一个点会被当成小数点,所以再写一个点代表方法调用console.log('abc'.toString());
console.log(true.toString());
console.log(Symbol().toString());
// 基本类型调用方法,会自动封装为对象
// 不能使用undefined.toString()或null.toString()会报错,因为没有对应的封装对象

You can see that only ordinary objects return what we want.
This is because ordinary objects directly call the toString method on the top prototype Object
And arrays, functions and these basic packaging objects inherit While Obejct
rewrites some methods
When looking for the toString method, the method on the own prototype chain must be found first
So we need to use call to call d on the top prototype Object.prototype toString method

The above is the content of JavaScript type checking and internal attributes [[Class]]. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn