Common knowledge points in Js (typeof, instanceof, dynamic properties, variable scope)

1. The constant representation of various types in Js: Number: number String: string Object: objec

2. The use of the typeof operator in Js: used to determine what type an object is, and the return value is a string (string form of constants of various types)

var message = "some string";

alert((typeof message) == String)
  {
   alert("OK");
  }  ——结果:false、ok

alert(typeof message == "string"); —true

3. typeof cannot accurately represent the type of the class (for example, the following example: the method Student is capitalized here, indicating that a class is declared, but the object is printed when the typeof operator is used)

function Student(n, a)
  {
   this.name = n;
   this.age = a;
  }
  var stuA = new Student('张三', 23);
  alert(typeof stuA)//object
  alert(stuA.name + ":" + stuA.age);  //张三:23

4、

5. The dynamic attributes of the object (created out of nothing), it is required to declare an object first and then have the dynamic phase attributes of the object

6. Understand the scope of functions in Js (importent)

<script type="text/javascript">
      var color = "blue"; // window.color 
     function changeColor(){
             var anotherColor = "red" ;
             // The var keyword is not used, then its scope is Window, So never write 
            myColor = "yellow"; // window.myColor
            
            function swapColors(){
                var tempColor = anotherColor;
                anotherColor = color;
                color = tempColor;
            }
            swapColors();
        }
        changeColor();
         alert("Color is now " + color);//red
        alert(window.color); //red
        alert(window.myColor);//yellow

</script>

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325038204&siteId=291194637