百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>JS教程> JavaScript类型推断的办法介绍(代码示例)
分享文章到:

JavaScript类型推断的办法介绍(代码示例)

发布时间:09/01 来源:未知 浏览: 关键词:
本篇文章给大家带来的内容是关于JavaScript类型推断的办法介绍(代码示例),有必然的参照 价值,有需要的伴侣可以参照 一下,但愿对你有所帮忙。

JS数据类型推断

有的时候需要推断数据类型,应对各种复杂的逻辑推断,先来个咱们最常用的。

1.typeof

typeof操纵符返回一个字符串,并表示该变量的类型。

typeof oper / typeof (operand)

var testString = 'adus',
    testArray = [],
    testBoolean = true,
    testNumber = 0,
    testObject = {},
    testNull = null,
    testUndefined = undefined
    
console.log(typeof testString);//string
console.log(typeof testArray);//object
console.log(typeof testBoolean);//boolean
console.log(typeof testNumber);//number
console.log(typeof testObjec);//object
console.log(typeof testNull);//object
console.log(typeof testUndefined);//undefined

当然不只要上面的这些根基类型,还有下面这些:

Symbol (ECMAScript 6 新增) "symbol"

宿主对象(由JS环境供给) Implementation-dependent

函数对象([[Call]] 在ECMA-262条目中实现了) "function"

咦?有点不合错误,为什么数组、null等都是object呢???请往下看

Function或者Array这些类型的实例的时候,其实都是基于Object实例停止的一种扩展。只是多了一些特有属性。

在 JavaScript 最初的实现中,JavaScript 中的值是由一个表示类型的标签和实际数据值表示的。对象的类型标签是 0。

由于 null 代表的是 空指针(大多数平台下值为 0x00)。

因此,null的类型标签也成为了 0,typeof null就错误的返回了"object"。(reference)

2.instanceof

instanceof运算符用于测试结构函数的prototype属性可否显现在对象的原型链中的任何位置,通俗点说就是一个变量可否某个对象的实例。

object instanceof constructor

object 要检测的对象 / constructor 结构函数

function fnc(){}
var newFnc = new fnc();
console.log(newFnc.__proto__ == fnc.prototype);//true
console.log( newFnc instanceof fnc ) //true


/*String对象和Date对象都属于Object类型和一些非凡状况*/

var simpleStr = "This is a simple string"; 
var myString  = new String();
var newStr    = new String("String created with constructor");
var myDate    = new Date();
var myObj     = {};
var myNonObj  = Object.create(null);

simpleStr instanceof String; // 返回 false, 检查原型链会寻到 undefined
myString  instanceof String; // 返回 true
newStr    instanceof String; // 返回 true
myString  instanceof Object; // 返回 true

myObj instanceof Object;    // 返回 true, 尽管原型没有定义
({})  instanceof Object;    // 返回 true, 同上
myNonObj instanceof Object; // 返回 false, 一种创立对象的办法,这种办法创立的对象不是Object的一个实例

myString instanceof Date; //返回 false

myDate instanceof Date;     // 返回 true
myDate instanceof Object;   // 返回 true
myDate instanceof String;   // 返回 false

3.Object.prototype.toString()

每个对象都有一个toString()办法,当该对象被表示为一个文本值时,或者一个对象以预测的字符串方式援用时主动调取。默许状况下,toString()办法被每个Object对象继承。假如此办法在自定义对象中未被覆盖,toString() 返回 "[object type]",其中type是对象的类型。

Function.prototype.call( thisArg ) / Function.prototype.apply( thisArg )

传递要检查的对象作为第一个参数,称为thisArg。

var toString = Object.prototype.toString;

toString.call(new Date); // [object Date]
toString.call(new String); // [object String]
toString.call(Math); // [object Math]

//Since JavaScript 1.8.5
toString.call(undefined); // [object Undefined]
toString.call(null); // [object Null]

在适宜的场景用适当的办法,会很省心,也能包管代码的简约、强健。

以上就是JavaScript类型推断的办法介绍(代码示例)的具体内容,更多请关注百分百源码网其它相关文章!

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有151人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板