JS模块机制

 代码模块

Require

.1. 加载指定代码 并执行

  1. 第二次加载不执行  ,但会执行module.exports

require返回值  module.exports= “dddd”,函数,

 Var 模块名=require(“模块文件”)

模块名.函数

 

This 机制

函数.call 显式的传递this

function my_func(){

  console.log(this);

}

my_func.call({});

 

var tools={

   myfunc:my_func

}

tools.myfunc();  //.函数key()  -->  this - ->  表隐式

 

//强制绑定this

var newf=my_func.bind({name:"gaga到底"});

newf();

//This 回调机制

/*

在函数里面访问this,this由调用的环境来决定 不确定 不使用

显式的传递this,函数.call(this对象,参数)

隐式的传递this,.key函数(参数),   this-- >

强制bind this 优先级最高.

*/

参考文章

别再为了this发愁了------JS中的this机制

https://www.cnblogs.com/front-Thinking/p/4364337.html

 

 

New与构造函数

每一个函数都有一个表 prototype

 

New  类似 其他语言创建一个类

1创建一个新表

2 创建一个新表 this 传递 调用person函数

3 person构造函数里面prototype这个表赋值到新的表的.__proto___

4 返回这个新表

模拟了 类和 实例

 

console.log(Math.PI);//prototype); 

function person (name,age){ 

  this.name=name;

this.age=age;

}
person.prototype.get_age=function(){
  return this.age;
}

console.log(person.prototype);

var b=new person("b",220) 

console.log(person.prototype);

console.log(b);

console.log(b.__proto__);

console.log(b.get_age());

 

 

 

 

 

猜你喜欢

转载自www.cnblogs.com/iflii/p/10189797.html