There are several ways to determine the data type
1. Unary operator typeOf
2. Relational operator instanceof
3. constructor attribute
4. prototype attribute
1. typeof
The return values of typeof are as follows
Type | Structure |
---|---|
Undefined | "undefined" |
Null |
"object" (see below) |
Boolean value | "boolean" |
Value | "number" |
String | "string" |
Symbol (new in ECMAScript 6) | "symbol" |
Host object (provided by JS environment, such as browser) | Implementation-dependent |
Function object (implements [[Call]] in ECMA-262 terms) | "function" |
Any other object | "object" |
Simple and crude method, just look at the code
// 以下代码在版本 Google Chrome 45.0.2454.101 m 中测试通过 // Numbers console.log(typeof 37 === 'number'); console.log(typeof 3.14 === 'number'); console.log(typeof Math.LN2 === 'number'); console.log(typeof Infinity === 'number'); console.log(typeof NaN === 'number'); // 尽管NaN是"Not-A-Number"的缩写,意思是"不是一个数字" console.log(typeof Number(1) === 'number'); // 不要这样使用! // Strings console.log(typeof "" === 'string'); console.log(typeof "bla" === 'string'); console.log(typeof (typeof 1) === 'string'); // console.log(typeof返回的肯定是一个字符串 console.log(typeof String("abc") === 'string'); // 不要这样使用! // Booleans console.log(typeof true === 'boolean'); console.log(typeof false === 'boolean'); console.log(typeof Boolean(true) === 'boolean'); // 不要这样使用! // Symbols console.log(typeof Symbol() === 'symbol'); console.log(typeof Symbol('foo') === 'symbol'); console.log(typeof Symbol.iterator === 'symbol'); // Undefined console.log(typeof undefined === 'undefined'); console.log(typeof blabla === 'undefined'); // 一个未定义的变量,或者一个定义了却未赋初值的变量 // Objects 使用Array.isArray或者Object.prototype.toString.call方法可以从基本的对象中区分出数组类型 console.log(typeof {a:1} === 'object'); console.log(typeof [1, 2, 4] === 'object'); console.log(typeof /^[a-zA-Z]{5,20}$/ === 'object'); console.log(typeof {name:'wenzi', age:25} === 'object'); console.log(typeof null === 'object');//true // 下面的容易令人迷惑,不要这样使用! console.log(typeof new Boolean(true) === 'object'); console.log(typeof new Number(1) === 'object'); console.log(typeof new Date() === 'object'); console.log(typeof new String("abc") === 'object'); console.log(typeof new Error() === 'object'); // 函数 console.log(typeof function(){} === 'function'); console.log(typeof Math.sin === 'function');
typeof can only check the above 7 types
2. instanceof
The instanceof operator is used to identify the type of object being processed, requiring developers to explicitly confirm that the object is a specific type
1. instanceof has nothing to do with constructor
var A = function() {}; A.prototype = {}; var B = {}; console.log(A.constructor);//function Function() { [native code] } console.log(B.constructor);//function Object() { [native code] } var a = new A(); A.prototype = {}; var b = new A(); b.constructor = A.constructor; console.log(a.constructor === A);//false console.log(a.constructor);//function Object() { [native code] } console.log(typeof A);//function Object() { [native code] } console.log(a.constructor === b.constructor);//false console.log(b.constructor);//function Function() { [native code] } console.log(a instanceof A);//false console.log(b instanceof A);//true
2. Instanceof is also called the relational operator, which can be used to determine whether the prototype attribute of a certain constructor exists on the prototype chain of another object to be detected
var str = new String("hello world"); console.log(str instanceof String);//true console.log(String instanceof Function);//true console.log(str instanceof Function);//false
Why does the third output return false? Original address: A question about instanceof in Javascript
//表达式一的指向 console.log(str.__proto__ === String.prototype);//true console.log(str instanceof String); //true //表达式二的指向 console.log(String .__proto__ === Function.prototype);//true console.log(String instanceof Function);//true //表达式三的指向 console.log(str .__proto__ === String.prototype);//true console.log(str .__proto__.__proto__ === String.prototype.__proto__);//true console.log(str .__proto__.__proto__ === Object.prototype);//true console.log(str .__proto__.__proto__.__proto__ === null);//true console.log(str instanceof Object);//true console.log(str instanceof Function);//false
Let’s look at another complicated usage
console.log(Object instanceof Object);//true console.log(Function instanceof Function);//true console.log(Number instanceof Number);//false console.log(String instanceof String);//false console.log(Function instanceof Object);//true console.log(Foo instanceof Function);//true console.log(Foo instanceof Foo);//false
Why, why is this? You need to understand the following meaning
1. How is this operator defined in the language specification?
2. JavaScript prototypal inheritance mechanism
Object instanceof Object
// 为了方便表述,首先区分左侧表达式和右侧表达式 ObjectL = Object, ObjectR = Object; console.log(ObjectL instanceof ObjectR);//true
// 下面根据规范逐步推演 console.log(ObjectL.__proto__ === Function.prototype); //true console.log(ObjectL.__proto__.__proto__ === Object.prototype);//true
Function instanceof Function
FunctionL = Function, FunctionR = Function; console.log(FunctionL instanceof FunctionR);//true console.log(FunctionL.__proto__ === Function.prototype); //true <strong>Foo instanceof Foo </strong> function Foo(){} var foo = new Foo(); FooL = Foo, FooR = Foo; console.log(FooL instanceof FooR);//false console.log(FooL.__proto__ === Function.prototype );//true console.log(FooL.__proto__.__proto__ === Object.prototype );//true console.log(FooL.__proto__.__proto__.__proto__ === null );//true
Application of instanceof in Dojo inheritance mechanism
In JavaScript, there is no concept of multiple inheritance, just like Java. But when using declare to declare a class in Dojo, it is allowed to inherit from multiple classes
dojo.declare("Aoo",null,{}); dojo.declare("Boo",null,{}); dojo.declare("Foo",[Aoo,Boo],{}); var foo = new Foo(); console.log(foo instanceof Aoo);//true console.log(foo instanceof Boo);//false console.log(foo.isInstanceOf(Aoo));//true console.log(foo.isInstanceOf(Boo));//true
instanceof and multiple global objects (interaction between multiple frames or multiple windows)
In a browser, our script may need to interact between multiple windows. Multiple windows mean multiple global environments, and different global environments have different global objects and thus different built-in type constructors. This may cause some problems. For example, the expression [] instanceof window.frames[0].Array will return false because Array.prototype !== window.frames[0].Array.prototype, so you must use Array.isArray(myObj) or Object. prototype.toString.call(myObj) === "[object Array]" to determine whether myObj is an array.
// 以下代码在版本 Google Chrome 45.0.2454.101 m 中测试通过 // Numbers console.log(37 instanceof Number);//false console.log( 3.14 instanceof Number);.//false console.log( Math.LN2 instanceof Number);//false console.log( Infinity instanceof Number);//false console.log( NaN instanceof Number); // false尽管NaN是"Not-A-Number"的缩写,意思是"不是一个数字" console.log( Number(1) instanceof Number); // false不要这样使用! // Strings console.log( "" instanceof String);// false console.log( "bla" instanceof String);// false console.log( ( 1) instanceof String); // falseconsole.log(返回的肯定是一个字符串 console.log( String("abc") instanceof String); // false 不要这样使用! // Booleans console.log( true instanceof Boolean);// false console.log( false instanceof Boolean);// false console.log( Boolean(true) instanceof Boolean); //false 不要这样使用! // Symbols console.log( Symbol() instanceof Symbol);// false console.log( Symbol("foo") instanceof Symbol);// false console.log( Symbol.iterator instanceof Symbol);// false // Undefined var blabla; //console.log( undefined instanceof Undefined);// Uncaught ReferenceError: Undefined is not defined //console.log( blabla instanceof Undefined); // Uncaught ReferenceError: Undefined is not defined console.log( undefined instanceof Object);// false console.log( blabla instanceof Object);// false // Objects 使用Array.isArray或者Object.prototype.toString.call方法可以从基本的对象中区分出数组类型 console.log( {a:1} instanceof Object);//true console.log( [1, 2, 4] instanceof Object);//true console.log( /^[a-zA-Z]{5,20}$/ instanceof Object);//true console.log( {name:'wenzi', age:25} instanceof Object);//true console.log( null === Object);//false // 下面的容易令人迷惑,不要这样使用! console.log( new Boolean(true) instanceof Object);//true console.log( new Number(1) instanceof Object);//true console.log( new Date() instanceof Object);//true console.log( new String("abc") instanceof Object);//true console.log( new Error() instanceof Object);//true // 函数 console.log( function(){} instanceof Function );//true console.log( Math.sin instanceof Function);//true
Note: undefined and null are detected Object types, because there are no global types of Undefined and Null in js, and number, string and boolean cannot detect their types
3. constructor
When using instanceof to detect variable types, we cannot detect the types of number, 'string', and bool. Therefore, we need to find another way to solve this problem
Object.prototype.constructor returns a reference to the function that created the prototype of the object. Note that the value of this property is the function itself, not a string containing the function name. For primitive values (such as 1, true or "test"), this property is read-only, and all objects inherit a constructor property from its prototype
Constructor is originally a property on the prototype object, pointing to the constructor. However, according to the order in which the instance object searches for attributes, if there is no instance attribute or method on the instance object, it will be searched on the prototype chain. Therefore, the instance object can also use the constructor attribute
function Person(){ } var Tom = new Person(); console.log(Tom.constructor === Person);//true
However, please note that the constructor attribute can be modified, which will cause the detected results to be incorrect
function Person(){ } function Student(){ } Student.prototype = new Person(); var John = new Student(); console.log(John.constructor==Student); // false console.log(John.constructor==Person); // true
Change the value of the constructor property of this object
function Type() { }; var types = [ new Array, [], new Boolean, true, // remains unchanged new Date, new Error, new Function, function(){}, Math, new Number, 1, // remains unchanged new Object, {}, new RegExp, /(?:)/, new String, "test" // remains unchanged ]; for(var i = 0; i < types.length; i++) { types[i].constructor = Type; types[i] = [ types[i].constructor, types[i] instanceof Type, types[i].toString() ]; }; console.log( types.join("\n") );
Except undefined and null, the type of other types of variables can be determined using constructor
4. The universal Object.prototype.toString.call
Use toString() method to detect object type
function Type() { }; var toString = Object.prototype.toString; console.log(toString.call(new Date) === '[object Date]');//true console.log(toString.call(new String) ==='[object String]');//true console.log(toString.call(new Function) ==='[object Function]');//true console.log(toString.call(Type) ==='[object Function]');//true console.log(toString.call('str') ==='[object String]');//true console.log(toString.call(Math) === '[object Math]');//true console.log(toString.call(true) ==='[object Boolean]');//true console.log(toString.call(/^[a-zA-Z]{5,20}$/) ==='[object RegExp]');//true console.log(toString.call({name:'wenzi', age:25}) ==='[object Object]');//true console.log(toString.call([1, 2, 3, 4]) ==='[object Array]');//true //Since JavaScript 1.8.5 console.log(toString.call(undefined) === '[object Undefined]');//true console.log(toString.call(null) === '[object Null]');//true
Attached is the judgment function. How much do you know about the data types in Javascript
5. Implementation of jquery jquery: "1.8.2",
jquery provides a $.type interface, take a look at the code
var m = Object.prototype.toString //501行 E = {};//512行 isFunction: function(a) { //645行 return p.type(a) === "function" }, isArray: Array.isArray || function(a) { return p.type(a) === "array" } , isWindow: function(a) { return a != null && a == a.window }, isNumeric: function(a) { return !isNaN(parseFloat(a)) && isFinite(a) }, type: function(a) { return a == null ? String(a) : E[m.call(a)] || "object" }, isPlainObject: function(a) { if (!a || p.type(a) !== "object" || a.nodeType || p.isWindow(a)) return !1; try { if (a.constructor && !n.call(a, "constructor") && !n.call(a.constructor.prototype, "isPrototypeOf")) return !1 } catch (c) { return !1 } var d; for (d in a) ; return d === b || n.call(a, d) }, isEmptyObject: function(a) { var b; for (b in a) return !1; return !0 },
It can be seen that jquery is implemented using Object.prototype.toString.call

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
