1, IndexOf ()
This method is used to return the position of the first occurrence of a specified string value in a string.
Syntax:indexOf (Searchvalue,fromindex); two parameters, parameter one represents the query string value, parameter two optional represents the starting position of the start query, if not write the default starting from the first character query
1 var string = "abcdeadhu390u09"; 2 Console.log (String.IndexOf ("D")); // 3 Note the subscript value starting from 0 3 console.log (String.IndexOf ("D")); //
The above code has only one argument to print out the first occurrence of the "D", "D" string values, the value of the output is different, indicating that the indexof () method is case-sensitive.
The following code passed two parameters, according to the preceding argument two to indicate the starting position of the query, so from the fifth bit start query "D" where the first occurrence of the position, the query does not return 1
Console.log (String.IndexOf ("D", 4)); // -1
The IndexOf () method is also commonly used to determine the type of browser, which uses the following:
1 if (Navigator.userAgent.indexOf (" Firefox ") >0 2 return " Firefox "; 3 }else if (Navigator.userAgent.indexOf ("Chrome" ) >0) { 4 return "Chrome" 5 }else if (Navigator.userAgent.indexOf ("Opera" 6 return "Opera" 7 }
Navigator.userAgent.indexOf ("Opera" Span style= "font-size:14px" > query, if the open browser is open gate returns a value greater than 0, otherwise 1
2, instanceof
This operator is used to detect the type of object
syntax: object instanceof Constructor object represents the object to be detected, parameter constructor
function Person () {} var New Person ();
object.prototypeof (Dave) ===person.prototypeinstanceof person); // true
It can be understood that instanceof detects whether the Constructor.prototype exists on the parametric object prototype chain. Returns true if present
It says a more general usage, now look at the use of inheritance
1 functionPerson () {};2 functionStudent () {};3Student.prototype =NewPerson ();//the prototype inheritance in JS4 varDave =NewStudent ();5Console.log (DaveinstanceofStudent);//true6Console.log (Daveinstanceofperson);//true
The preceding section of code determines if Dave is an instance of student and whether it is an instance of its parent type
3, typeof
The operator is used to detect the base data type
1Console.log (typeof("Json"));//string2Console.log (typeof(2));// Number3Console.log (typeof(true));//Boolean4Console.log (typeof({a:1}));//Object5Console.log (typeof(function(){}));//function
6 Console.log (typeof(undefined)); // undefined
Before ES6 the TypeOf return value is the six kinds listed above:string, Number, Bollean, object, function, undefined; ES6 came out and added a symbol .
Console.log (typeof(Symbol ())); // symbol
4, ValueOf ()
The method returns the original value of the Boolean object
Syntax: booleanobject.valueof ()
1 var New Boolean (true); 2 Console.log (boo.valueof ());
Add small points of knowledge that are not relevant to this chapter:
A little bit of "= =" and "= = =" are interspersed here.
1 var a = undefined; 2 var NULL ; 3 Console.log (a==b); // true 4 Console.log (a===b); // false
Null and undefined return true in "= =" cases, because they are similar values that return false in "= = =" cases, because they are not the same type of values.
It's special.
1 console.log (Nan==nan); // false 2 Console.log (3==nan); // false
If an operator is Nan, returns false in the "= =" case, even if two operands are Nan, and false in the "= =" case
1 console.log (false==0); // true 2 Console.log (true==1); // true 3 Console.log (true==2); // false
True returns true with 1 when operator is "= ="
1 console.log (null==0); // false 2 Console.log (undefined==0); // false
IndexOf, instanceof, typeOf, valueof detailed