码迷,mamicode.com
首页 > 编程语言 > 详细

Javascript中的typeof和instanceof

时间:2016-08-30 23:08:17      阅读:549      评论:0      收藏:0      [点我收藏+]

标签:javascript


typeof 是一元操作符,而instanceof是二元操作符;

typeof 操作的是一个变量,而instanceof前面是一个变量,后面是一个类型;

typeof 返回的是一个字符串,而instanceof 返回的是一个布尔值。


1、typeof()


http://www.cnblogs.com/jikey/archive/2010/05/05/1728337.html


typeof 是一个一元运算,放在一个运算数之前,运算数可以是任意类型。
它返回值是一个字符串,该字符串说明运算数的类型。,typeof一般只能返回如下几个结果:number,boolean,string,function,object,undefined。 


示例

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <title>Javascript测试</title>
    <script type="text/javascript">
        document.write ("typeof(1): "+typeof(1)+"<br />");
        document.write ("typeof(NaN): "+typeof(NaN)+"<br />");
        document.write ("typeof(Number.MIN_VALUE): "+typeof(Number.MIN_VALUE)+"<br />");
        document.write ("typeof(Infinity): "+typeof(Infinity)+"<br />");
        document.write ("typeof(\"123\"): "+typeof("123")+"<br />");
        document.write ("typeof(true): "+typeof(true)+"<br />");
        document.write ("typeof(window): "+typeof(window)+"<br />");
        document.write ("typeof(Array()): "+typeof(new Array())+"<br />");
        document.write ("typeof(function(){}): "+typeof(function(){})+"<br />");
        document.write ("typeof(document): "+typeof(document)+"<br />");
        document.write ("typeof(null): "+typeof(null)+"<br />");
        document.write ("typeof(eval): "+typeof(eval)+"<br />");
        document.write ("typeof(Date): "+typeof(Date)+"<br />");
        document.write ("typeof(sss): "+typeof(sss)+"<br />");
        document.write ("typeof(undefined): "+typeof(undefined)+"<br />")
    </script>
</head>
<body>
    
</body>
</html>

结果

技术分享



typeof

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof


The typeof operator returns a string indicating the type of the unevaluated operand.

技术分享

技术分享

示例

// Numbers
typeof 37 === ‘number‘;
typeof 3.14 === ‘number‘;
typeof(42) === ‘number‘;
typeof Math.LN2 === ‘number‘;
typeof Infinity === ‘number‘;
typeof NaN === ‘number‘; // Despite being "Not-A-Number"
typeof Number(1) === ‘number‘; // but never use this form!


// Strings
typeof "" === ‘string‘;
typeof "bla" === ‘string‘;
typeof (typeof 1) === ‘string‘; // typeof always returns a string
typeof String("abc") === ‘string‘; // but never use this form!


// Booleans
typeof true === ‘boolean‘;
typeof false === ‘boolean‘;
typeof Boolean(true) === ‘boolean‘; // but never use this form!


// Symbols
typeof Symbol() === ‘symbol‘
typeof Symbol(‘foo‘) === ‘symbol‘
typeof Symbol.iterator === ‘symbol‘


// Undefined
typeof undefined === ‘undefined‘;
typeof declaredButUndefinedVariable === ‘undefined‘;
typeof undeclaredVariable === ‘undefined‘; 


// Objects
typeof {a:1} === ‘object‘;

// use Array.isArray or Object.prototype.toString.call
// to differentiate regular objects from arrays
typeof [1, 2, 4] === ‘object‘;

typeof new Date() === ‘object‘;


// The following is confusing. Don‘t use!
typeof new Boolean(true) === ‘object‘; 
typeof new Number(1) === ‘object‘; 
typeof new String("abc") === ‘object‘;


// Functions
typeof function(){} === ‘function‘;
typeof class C {} === ‘function‘;
typeof Math.sin === ‘function‘;

null

// This stands since the beginning of JavaScript
typeof null === ‘object‘;


In the first implementation of JavaScript, JavaScript values were represented as a type tag and a value. The type tag for objects was 0. null was represented as the NULL pointer (0x00 in most platforms). Consequently, null had 0 as type tag, hence the bogus typeof return value. (reference)

A fix was proposed for ECMAScript (via an opt-in), but was rejected. It would have resulted intypeof null === ‘null‘.





2、instanceof


instanceof

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof


The instanceof operator tests whether an object has  the prototype property of a constructor in its prototype chain.

技术分享

The instanceof operator tests presence of constructor.prototype in object‘s prototype chain.

// defining constructors
function C(){}
function D(){}

var o = new C();

alert(o instanceof C);// true, because: Object.getPrototypeOf(o) === C.prototype
alert(o instanceof D);// false, because D.prototype is nowhere in o‘s prototype chain
alert(o instanceof Object);// true
alert(C.prototype instanceof Object);// true

C.prototype = {};
var o2 = new C();
alert(o2 instanceof C);//true

alert(o instanceof C);// false, because C.prototype is nowhere in o‘s prototype chain anymore

D.prototype = new C();// use inheritance
var o3 = new D();
alert(o3 instanceof D);//true
alert(o3 instanceof C);//true






Javascript中的typeof和instanceof

标签:javascript

原文地址:http://lsieun.blog.51cto.com/9210464/1844372

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!