js basis - for variables, scope, memory

1, create a new keyword is a reference type;
eg. var box = new Object();
     box.name = "Linda"; // add a reference type attribute no problem
    alert(box.name);
2, the basic types of output undefined added property
eg. var box = "Linda";
     box.height = 185;
   alert(box.height);//undefined
3, the value of the variable copy: Copy primitive type value itself, copy the address reference type.
eg. Basic Copy variable type (i.e. two variable operation independently of each other)
     var str1 = "Lee";
       was str2 = str1;
       str1 = "hello";
       console.log(str1);//hello
       console.log(str2);//lee
eg. Copy reference type variable (Box and box2 are point to the same variable, if changed attribute name, and the value of box2.name of box.name will change accordingly)
        var str3 = new Object();
        str3.name = "Bob";
        was str4 = str3;
        console.log(str3.name);//Bob
        console.log(str4.name);//Bob
4, transmission parameters: the parameter is not passed by reference (local variables), directly by value, even if the variable basic points and reference types.
eg. basic types of transmission parameters
  function fn (num) {// num is a local variable
         a + = 10;
         Surely return;
       }
       var n = 50;
       was Result = fn (num);
       console.log(result);//60
       console.log (num); // 50 irrespective of local variables above
eg. transmission parameter reference type
 function fun(obj) {
          obj.address = "Sichuan";
        }
        var p = new Object();
        fun(p);
        console.log (p.address); // Sichuan
5, the type of detection:
1) typeof (typically for detecting the data type)
eg. was resorting = true;
        console.log(typeof ty);//boolean
2) instanceof (what type of object is detected: arrays, strings, etc.)
eg. was TY1 = [1, 25, 59, 265];
        console.log (ty1 instanceof Array); // detect whether the array is true
        was ty2 = {};
        console.log (ty2 instanceof Object); // detecting whether the object is true
        var ty3 = / g /;
        console.log (ty3 instanceof RegExp); // detecting whether the true positive
        var ty4 = new String("Linda");
6, scope and execution environment
Other data execution environment defines the function or variable have access to, and determine their own behavior.
Global execution environment is the most peripheral point to the environment, that is, the window object
     var box1 = "blue";
        function setBox(){
          console.log (box1); // access the global variables
        }
        setBox (); // perform functions
    var boxn="hello";
        function getBox(){
          console.log(window.boxn);//全局变量即window的属性
        }
        window.getBox();//全局函数即window的方法
eg.通过传参替换函数体内的局部变量,但是作用域仅限于函数体内的局部环境
  var boxn="hello";
        function getBox(boxn){//通过传参替换了全局变量
          console.log(boxn);//WORLD
        }
        getBox("WORLD");
        console.log(boxn);//hello
eg.每个函数调用时都会创建自己的执行环境,执行后把控制权交给上一级的执行环境
        var color="yellow";
        function setColor(){
          function getColor() { //getColor的执行环境在getColor内
            var color1 = "green";
            console.log(color);//yellow
            console.log(color1);//green
          }
          getColor();
        }
        setColor();
7、没有局部作用域:if语句、for语句
eg.var关键字在函数体内的意义
function number(number1,number2) {
          var sum = number1 + number2; //此时sum是局部变量,去掉var就是全局,报错
          return sum;
        }
        console.log(number(15,20));
        console.log(sum);//报错 sum is undefined
8、内存问题
javaScript会自行管理内存分配及无用内存的回收
一般来说确保占用最少的内存可让页面获得更好的性能,那么优化内存的最佳方案就是一旦数据不再有用,就将其设置为null来释放引用即解除应用.
eg.var p = {
   name:"hello"
  }
  p = null;

Guess you like

Origin www.cnblogs.com/LindaBlog/p/10984192.html