This is the 9th day of my participation in the August Wen Challenge.More challenges in August

typeof

Typeof outputs undefined for defined variables whose value is undefined, undefined for undefined and undefined variables with no assigned value.

So there are some security mechanisms, such as the following:

(function(){
    function Feature() {
        /*   */
    }
    function doSomething() {
        let helper = (typeofFeature ! = ='undefined')?
            Feature:
            function () {
                console.log("123")}let val  = helper()
    }
    doSomething()
})()
Copy the code

Feature is not a global variable, but it can be checked using typeof’s security mechanism, or using the properties of the Window object in the browser environment.

function doSomething2(Feature) {
    let helper = Feature||function () {}
    let val = helper()
}
Copy the code

The code is much cleaner

Seven built-in types JavaScript: null, and undefined, number, string, Boolean, symbol, and the object

Internal attributes [[Class]]

You can use the Object. The prototype. The toString () to access the data types in the kernel attribute definition:

console.log(Object.prototype.toString.call([1.2.3]))// Internal attributes - array
console.log(Object.prototype.toString.call(/regex-literal/i))// Internal attribute - regular
console.log(Object.prototype.toString.call(null))// Internal attribute -Null
Copy the code
Object wrapper

object wrapper

Since primitive types don’t have methods like length, toString(), etc., you need to wrap objects to access them

let a  = new Boolean(false)
console.log(typeof a)//Object automatically converts to true
console.log(a)
if(a){/ / a is true
  console.log("aaa")} -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -let a='abc'
let b = new String(a)/ / packaging
let c = Object(a)/ / packaging
console.log(typeof a,typeof b,typeof c)
console.log(b instanceof String ,c instanceof String )//true true
console.log(Object.prototype.toString.call(b),Object.prototype.toString.call(c))//[object String] [object String]

Copy the code
Use native function construction
  • Array(..)
  • Object(..)
  • Function(..)
  • RegExp(..)
  • Date(..)
  • Error(..)
  • Symbol(..)

One particular type is the Symbol type and the use of Symbol to implement the iterating properties of the type.

What is a Symbol

Symbol is a primitive data type. Symbol is a primitive data type. The Symbol() function returns a value of type Symbol, which has static attributes and static methods. Its static attributes expose several built-in member objects; Its static method exposes the global Symbol registry and is similar to the built-in object class, but as a constructor it is incomplete because it does not support the syntax: “new Symbol()”.

Symbol(” A “)===Symbol(” A “) //false can be used to construct unique attributes of objects, preventing duplication of attributes and collision of prototype chains.

Symbol is best known for its iterator property. Using this property, we can implement iterator properties for objects, such as for… of … To iterate over an object that implements symbol. iterator. Let’s look at it with a simple piece of code

 var randoms = {
  [Symbol.iterator]: function () {
    return {
      next: function () {
        return {
          value: Math.floor(Math.random() * 10),}},}},}var randoms_pool = []
for (var n of randoms) {
  console.log(n)
    randoms_pool.push(n)
    if (randoms_pool.length === 10) break
} 
console.log(randoms_pool)
Copy the code

In the code above we implement the symbol. iterator property for Randoms, which generates a random number for each iteration. This allows the object Randoms to be iterated over in an array.

Operators && | |? :

Priority: && > | | >? To:

Operators association in && and | | appeared many times, in left associated way: from left to right, so a & b & c will be processed for (a & b) && & c? : is the right correlation, namely A? b : c ? D: E will be treated as a? b :(c ? D: e), and another example of a right correlation is the “=” operator, var a,b,c; A = b= c=1 c=1 a = b= c=1 , c =… .

let a = 1;
let b = 'foo';
let c = false;
let d = a && b || c ? c ||b ? a: c && b : a;
//((a && b) || c) ? ((c || b) ? a :(c && b)) : a
console.log(d)
Copy the code