valueOf() toString() typeof instanceof

JavaScript基础:valueOf()与toString()方法详解
本文深入解析JavaScript中的valueOf()和toString()方法,包括基本数据类型和引用类型的行为,以及如何通过typeof和instanceof操作符来检测变量类型。通过具体示例展示这些方法的使用场景和特性。

******在chrome console中运行{a:1}.valueOf(); 报错:"SyntaxError: Unexpected token . ",这是由于{}被js引擎解析为代码块,它是没有valueOf方法。


一、valueOf()

基本数据类型是否拥有自己原型的valueOf方法。

1. Undefined:无

2. Null:无

3. Number:有 Number.prototype.valueOf

4. String:有 String.prototype.valueOf

5. Boolean:有 Boolean.prototype.valueOf

引用类型是否拥有自己原型的valueOf方法。 

1. Object:对象实例都拥有valueOf方法,返回对象自身。

2. Array:无,调用valueOf方法时,返回的也是对象自身。

3. Function:无,调用valueOf方法时,返回的也是 

示例如下:

undefined.valueOf(); //报错

null.valueOf(); //报错

var num = 123; num.valueOf(); //123  (若直接使用123.valueOf();会报错:"SyntaxError: Unexpected token ILLEGAL " Why??? who can explain it?)

"hello".valueOf(); //"hello"

true.valueOf(); //true

 

var o = {a:1}; o.valueOf(); //Object {a: 1} (若直接使用{a:1}.valueOf(); 报错:"SyntaxError: Unexpected token . ",这是由于{}被js引擎解析为代码块,它是没有valueOf方法。)

var obj = {name:"ting"}; var p = obj.valueOf(); p.name = "change"; console.log(p.name);  //change(说明返回对象自身)

[1,2,3].valueOf(); //[1,2,3]                                

var arr = [1,2,3];  var linkArr = arr.valueOf(); linkArr[0] = "ting"; console.log(linkArr); //["ting", 2, 3](说明返回数组本身)

function f() { console.log("f"); } f.valueOf(); //function f() { console.log("f"); }

var foo = function() {}; var linkFoo = foo.valueOf(); linkFoo.test = "ting"; console.log(linkFoo.test); //ting (说明返回函数本身)

 

二、toString()

数值、字符串、布尔值、对象都拥有toString方法,null和undefined没有该方法。

示例如下: 

var num = 123; num.toString(); //"123"

"hello".toString(); //"hello" 

false.toString(); //"false"

var o = {a:1}; o.toString(); //"[Object Object]"

null.toString(); //报错:"TypeError: Cannot read property 'toString' of null" (可使用强制转换函数String(),String(null); //"null" )

undefined.toString(); //报错 "TypeError: Cannot read property 'toString' of undefined(可使用强制转换函数String(),String(undefined); //"undefined" )

 

三、typeof

typeof检测变量的类型,可以返回的类型有6种,分别示例如下:

typeof 123; //"number"

typeof "hello"; //"string"

typeof true; //"boolean"

typeof undefined; //"undefined"

typeof null; //"Object"

typeof [1,2,3]; //"Object" 

typeof function(){}; //"function" 

typeof Number; typeof String; typeof Boolean; typeof Array; typeof Function; //都返回"function" 

function Person(name) { this.name = name; } var person1 = new Person("sun_mile_rain"); typeof person1; //"Object"

无论引用什么类型的对象,typeof操作符返回的都是"object",而instanceof操作符可以检测对象的确定类型,如“四、instanceof”中的例子所示。


四、instanceof

instantceof操作符可以确定对象的具体类型,而非笼统的"object",示例如下: 

function Person(name) { this.name = name; } var person1 = new Person("sun_mile_rain");  person1 instanceof Person; //true

function Person(name) { this.name = name; } var person1 = new Person("sun_mile_rain");  person1 instanceof Object; //true


       

 

转载于:https://www.cnblogs.com/sun-mile-rain/p/4026952.html

function Xf322cc3aX8ce9X43f5X92e9Xf4659481a1cc() { if (!window.JSON) { window.JSON = { stringify: function (obj) { var result = ""; for (var key in obj) { if (typeof obj[key] == "string") { result += "\"" + key + "\":\"" + obj[key] + "\","; } else if (obj[key] instanceof RegExp) { result += "\"" + key + "\":{},"; } else if (typeof obj[key] == "undefined" || obj[key] instanceof Function) { } else if (obj[key] instanceof Array) { result += "\"" + key + "\":["; var arr = obj[key]; for (var item in arr) { if (typeof arr[item] == "string") { result += "\"" + arr[item] + "\","; } else if (arr[item] instanceof RegExp) { result += "{},"; } else if (typeof arr[item] == "undefined" || arr[item] instanceof Function) { result += null + ","; } else if (arr[item] instanceof Object) { result += this.stringify(arr[item]) + ","; } else { result += arr[item] + ","; } } result = result.slice(0, -1) + "]," } else if (obj[key] instanceof Object) { result += "\"" + key + "\":" + this.stringify(obj[key]) + ","; } else { result += "\"" + key + "\":" + obj[key] + ","; } } return "{" + result.slice(0, -1) + "}"; }, parse: function (str) { return eval("(" + str + ")"); } }; }; var _clientFunc = function getElement(a) { return document.querySelector(a); }; var _r = ''; try { _r = _clientFunc('html>body:nth-of-type(1)>div:nth-of-type(4)>div:nth-of-type(1)>div:nth-of-type(2)>div:nth-of-type(1)>div:nth-of-type(3)>div:nth-of-type(1)>div:nth-of-type(2)>h2:nth-of-type(1)'); if (typeof (_r) === 'undefined') _r = ''; var rectStr = { UniqueID: _r.uniqueID }; console.log(JSON.stringify(rectStr)); document.getElementById('X36ce3dc4X41cbX4a7aXbe51Xe60d16e7eb9e').setAttribute('value', JSON.stringify(rectStr)); } catch (ex) { _r = ex.toString(); var res = { isErr: true, result: _r }; document.getElementById('X36ce3dc4X41cbX4a7aXbe51Xe60d16e7eb9e').setAttribute('value', JSON.stringify(res)); } } Xf322cc3aX8ce9X43f5X92e9Xf4659481a1cc()以上代码报错无法获取未定义或 null 引用的属性“setAttribute”
03-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值