javascript重难点

1、new 的运行机制

function ClassA(prop1,prop2){
  this.prop1 = prop1;
  this.prop2 = prop2;
  this.method1 = function(){
    console.log(this.prop1);
  }
ClassA.prototype.method2=function(){
  console.log(this.prop2);
}
var classA = new ClassA('prop1''prop2');

其中new ClassA()的运行机制
{
var obj = {};//定义一个空的对象
obj.proto = ClassA.prototype;//obj的指针指向ClassA的原型,还没有实现它本身构造函数的属性、方法
var result = ClassA.call(obj,’porp’);//实现本身构造函数的方法
return typeof result === ‘object’?result:obj;//返回obj的实例
}
2、继承

function ClassA(prop1,prop2) {
            this.prop1 = prop1;
            this.prop2 = prop2;
            this.method1 = function () {
                console.log('console.log classA.method1() = ' + this.prop1);
            };
        }
        ClassA.prototype.method2=function(){
            console.log('console.log classA.method2() = '+ this.prop2);
        };
        var classA = new ClassA('prop1','prop2');
        console.log("*******************This is a classA method*****************");
        console.log(ClassA.prototype);
        classA.method1();
        classA.method2();
        function ClassB(prop3,prop4){
            this.prop3 = prop3;
            this.prop4 = prop4;
            this.method3 = function() {
                console.log('console.log classB.method3() = ' + this.prop3);
            }
        }
        ClassB.prototype = new ClassA('prop1','prop2');//注意必须要先声明一个实例,才能给ClassB添加新的方法或属性,或者会被覆盖,返回给ClassB.prototype的是一个ClassA的对象,包括构造方法,和一个指向ClassA原型的指针_proto_
        ClassB.prototype.method4 = function(){
            console.log('console.log classB.method4() = ' + this.prop4);
        };
        var classB = new ClassB('prop3','prop4');
        console.log("*******************This is a classB method*****************");
        console.log(ClassB.prototype);
        console.log(ClassB.prototype.__proto__ );
        classB.method1();
        classB.method2();
        classB.method3();
        classB.method4();
    function ClassC(prop5,prop6) {
        ClassB.apply(this,arguments);
        this.prop5 = prop5;
        this.prop6 = prop6;
        this.method5 = function () {
            console.log('console.log classA.method1() = ' + this.prop5);
        };
    }
    ClassC.prototype = ClassB.prototype;
    ClassC.prototype.method6=function(){
        console.log('console.log classA.method6() = '+ this.prop6);
    };
    var classC = new ClassC('prop5','prop6');
    console.log("*******************This is a classC method*****************");
    console.log(ClassC.prototype);//有method1,method4,method6的定义
    classC.method1();
    classC.method2();
    classC.method3();
    classC.method4();
    classC.method5();
    classC.method6();

3、严格模式
在html中javascript有三种运行方式,第一种就是我们正常使用的,另外一种是过渡的,第三种是use strict;严格模式
严格模式的作用,消除一些不合理,效率慢的语法,使语法更容易看懂。
如何调用,只要在模块的第一行使用use strict;

"use strict";
  v = 1; // 报错,v未声明
  for(i = 0; i < 2; i++) { // 报错,i未声明
  }
  "use strict";
  var v = 1;
  with (o){ // 语法错误 
    v = 2;
  }
  function f(){
    return !this;
  } 
  // 返回false,因为"this"指向全局对象,"!this"就是false
  function f(){ 
    "use strict";
    return !this;
  } 
  // 返回true,因为严格模式下,this的值为undefined,所以"!this"为true
  function f(){
    "use strict";
    this.a = 1;
  };
  f();// 报错,this未定义
  "use strict";
  var x;
  delete x; // 语法错误
  var o = Object.create(null, {'x': {
      value: 1,
      configurable: true
  }});
  delete o.x; // 删除成功

3、闭包
先简单明了码代码为先
function parent(){
var a = 1;
function children(){
console.log(a);
}
}
这是一个简单函数,函数里面也有一个函数
网上找的闭包概念
1、闭包是函数里面的函数,在本质上就是将函数内部和外部连接起来的桥梁
2、闭包是此法上下文中引用了自由变量
3、闭包是由函数和其相关的引用环境组合而成的实体,这个环境包含了这个闭包创建时所能访问的所有局部变量
闭包作用
1、读取函数内部内容
2、让这些变量的值始终保持再内存中
3、加强模块化
4、减少代码
闭包有什么作用,自从有了let后,发现闭包就没有什么太大的作用了
闭包的好处:保存变量,封装函数,避免全局污染,在那些实际场景中应用,当我们封装一个插件的时候,可以采用闭包。
http://www.ruanyifeng.com/blog/2013/01/javascript_strict_mode.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值