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
valueOf() toString() typeof instanceof的更多相关文章
- 【JavaScript中typeof、toString、instanceof、constructor与in】
JavaScript中typeof.toString.instanceof.constructor与in JavaScript 是一种弱类型或者说动态语言.这意味着你不用提前声明变量的类型,在程序运行 ...
- typeof 、Object.prototype.toString和 instanceof
数据类型 js 基本类型包括:Undefined symbol null string boolean number js 引用类型包括:object array Date RegExp typeo ...
- valueof toString
valueof toString add(1)(2) // 3 add(1, 2, 3)(10) // 16 add(1)(2)(3)(4)(5) // 15 function add() { var ...
- 小tip:关于typeof,instanceof,toString(),valueOf(),toLocaleString(),join(),reverse(),sort(),pop(),push(),shift(),unshift()
typeof:用于检测一个变量是否是基本数据类型.instanceof用于检测某引用对象是什么类型的对象. var s = "Nicho"; var b = true; var n ...
- JavaScript中typeof、toString、instanceof、constructor与in
JavaScript 是一种弱类型或者说动态语言.这意味着你不用提前声明变量的类型,在程序运行过程中,类型会被自动确定. 这也意味着你可以使用同一个变量保存不同类型的数据. 最新的 ECMAScrip ...
- typeof + instanceof+toString+constructor什么推理javascript数据类型
一个.typeof JS这些变量是弱类型(这是弱类型)的,它可以不管用来存储数据的类型的. typeof 数据类型可用于检测给定的变量.可能的返回值: 1. 'undefined' --- 这个值没有 ...
- JavaScript的三种类型检测typeof , instanceof , toString比较
1.typeof typeof是js的一个操作符,在类型检测中,几乎没有任何用处. typeof 返回一个表达式的数据类型的字符串,返回结果为javascript中的基本数据类型,包括:number. ...
- javascript篇-typeof,instanceof,constructor,toString判断数据类型的用法和区别
javascript基本数据类型有:string,number,Boolean,undefined,null 引用类型(复杂类型):object, ES6中新增了一种数据类型:Symbol 以上数据类 ...
- 类型判断----小白讲解typeof,instanceof,Object.prototype.toString.call()
1.typeof只能判断基本类型数据, 例子: typeof 1 // "number" typeof '1' // "string" typeof true ...
随机推荐
- .net环境下ckeditor与ckfinder中文文件链接乱码的问题
.net环境下ckeditor与ckfinder中文文件链接乱码的问题 将ckfinder.js中的getUrl:function(){return this.folder.getUrl()+enco ...
- 注册、卸载DLL
注册.卸载DLL,一般命令写在bat文件中,下面以注册.卸载SWFToImage.DLL为例. 1.注册文件(Install.bat)内容: REM copying files to the syst ...
- MapReduce的模式、算法和用例
英文原文:<MapReduce Patterns, Algorithms, and Use Cases> https://highlyscalable.wordpress.com/2012 ...
- 在FireFox中安装Selenium IDE
第二步:点击查看更多,查找Selenium IDE,安装 第三步:安装好后,在顶部的工具栏里点击"工具",弹出的选项框里出现Selenium IDE,安装完毕.
- C++string中用于查找的find系列函数浅析
总述: 以下所讲的所有的string查找函数,都有唯一的返回类型,那就是size_type,即一个无符号整数(按打印出来的算).若查找成功,返回按查找规则找到的第一个字符或子串的位置:若查找 ...
- c# this关键字的理解
this关键字引用类的当前实例 1/限定被相似的名称隐藏的成员 2/将对象作为参数传递到其他方法 3/声明索引器 实际案例参考: //成员类 public class Employee { priva ...
- Oracle_双机备份
1.dataguard http://jingyan.baidu.com/article/f96699bb956ef2894e3c1b39.html http://blog.itpub.net/262 ...
- window svn链接
我学会怎么建立window SVN服务器了 今天,终于学会怎么自己搭建SVN服务了,以前一直用的都是公司的SVN服务,没接触过,觉得很神秘,曾经我一个同事弄了好几天,也没搭成,对我打击挺大的:( 觉得 ...
- Spring+Mybatis 手动控制事务
public boolean testDelete(String jobCode) throws Exception { boolean flag = false; //1.获取事务控制管理器 Dat ...
- fiddler,https抓包设置
1.fiddler 2 汉化版本不支持https证书下载,需要下载fiddler 4版本进行验证 若fiddler 2版本,可能存在无法访问Pc端fiddler返回页面,无法下载证书 2.打开Fidd ...