美文网首页
indexOf、instanceOf、typeOf、valueO

indexOf、instanceOf、typeOf、valueO

作者: 云凡的云凡 | 来源:发表于2020-10-31 14:01 被阅读0次

1、indexOf()
该方法用来返回某个指定的字符串值在字符串中首次出现的位置。
语法:indexOf(searchvalue,fromindex);两个参数,参数一表示查询的字符串值,参数二可选表示开始查询的起始位置,若不写默认从首字符开始查询

var string = "abcdeADhu390u09";
console.log(string.indexOf("d"));//3  注意下标值从0开始
console.log(string.indexOf("D"));//6 

上面代码都只有一个参数分别打印出"d"、"D"字符串值首次出现的位置,发现输出的值不同,说明indexOf()方法对大小写有区分。

下面这段代码传入两个参数,根据前面说的参数二表示查询的起始位置,所以从第五位开始查询"d"首次出现的位置,查询不到返回-1

console.log(string.indexOf("d",4));//-1

indexOf()方法还常用来判断浏览器的类型,其用法如下:

if(navigator.userAgent.indexOf("Firefox")>0){
     return "Firefox";
 }else if(navigator.userAgent.indexOf("Chrome")>0){
     return "Chrome";
 }else if(navigator.userAgent.indexOf("Opera")>0){
     return "Opera";
 }

以navigator.userAgent.indexOf("Opera")查询来讲,若打开的浏览器是欧朋则返回一个大于0的值,否则返回-1

2、instanceOf()
该运算符用来检测对象的类型
语法:object instanceof constructor 参数object表示要检测的对象,参数constructor表示某个构造函数
检测某个变量是不是某个构造函数的实例,结果为布尔类型。

function Person(){}
var Dave = new Person();
//Object.prototypeOf(Dave)===Person.prototype
console.log(Dave instanceof Person);//true
var a=new Array();
alert(a instanceof Array); // true

可以这样理解:instanceof检测constructor.prototype是否存在于参数object原型链上。若存在返回true
上面说的是较常规的用法,现在来看看在继承中的用法

function Person(){};
function Student(){};
Student.prototype = new Person();//js中的原型继承
var Dave = new Student();
console.log(Dave instanceof Student);//true
console.log(Dave instanceof Person);//true

上面一段代码判断Dave是否是Student的实例,并且是否是其父类型的实例

3、typeof()
该运算符用来检测基本数据类型,打印出来的都是字符串

console.log(typeof("Json"));//string
console.log(typeof(2));//number
console.log(typeof(true));//boolean
console.log(typeof({a:1}));//object
console.log(typeof(function(){}));//function
console.log(typeof(undefined));//undefined

在ES6之前typeof返回值就是上面列出的六种:string、number、bollean、object、function、undefined;ES6出来后又增加了一种symbol

console.log(typeof(Symbol()));//symbol

4、valueOf()
用于返回指定对象的原始值。
该方法属于Object对象,由于所有的对象都"继承"了Object的对象实例,因此几乎所有的实例对象都可以使用该方法。所有主流浏览器均支持该函数。

语法

object.valueOf()

返回值

valueOf()函数返回指定对象的原始值。

JavaScript的许多内置对象都重写了该函数,以实现更适合自身的功能需要。因此,不同类型对象的valueOf()方法的返回值和返回值类型均可能不同。

       //Array:返回数组对象本身
        var array = ["CodePlayer", true, 12, -5];
        document.writeln(array.valueOf() === array);// true
        document.writeln(array.valueOf());// CodePlayer,true,12,-5
      // Date:当前时间距1970年1月1日午夜的毫秒数
        var date = new Date(2020, 10, 30, 21, 15, 00, 100);
        document.writeln(date.valueOf());// 1606742100100
       // Number:返回数字值
        var num = 3.1415926;
        document.writeln(num.valueOf());// 3.1415926

        // 布尔:返回布尔值true或false
        var bool = true;
        document.writeln(bool.valueOf() === bool);// true

        // new一个Boolean对象
        var newBool = new Boolean(true);

        // valueOf()返回的是true,两者的值相等
        document.writeln(newBool.valueOf() == newBool);// true
           // 但是不全等,两者类型不相等,前者是boolean类型,后者是object类型
        document.writeln(newBool.valueOf() === newBool);// false
 // Function:返回函数本身
        function foo() {
        }
        document.writeln(foo.valueOf() === foo);// true
        var foo2 = new Function("x", "y", "return x + y;");
        document.writeln(foo2.valueOf() === foo2);// true

        // Object:返回对象本身
        var obj = { name: "张三", age: 18 };
        document.writeln(obj.valueOf() === obj);// true

        // String:返回字符串值
        var str = "http://www.365mini.com";
        document.writeln(str.valueOf() === str);// true

        // new一个字符串对象
        var str2 = new String("http://www.365mini.com");

        // 两者的值相等,但不全等,因为类型不同,前者为string类型,后者为object类型
        document.writeln(str2.valueOf() === str2);// false


该方法返回Boolean对象的原始值
语法:booleanObject.valueOf()

        var boo = new Boolean(true);
        console.log(boo.valueOf()); //true

相关文章

网友评论

      本文标题:indexOf、instanceOf、typeOf、valueO

      本文链接:https://www.haomeiwen.com/subject/vxqjvktx.html