///模块///
1、引用其他模块require
a.如果是第一次调用,则加载并执行
b.每个模块由module.exports导出的对象
c.每次require时,都会返回module.exports
//main.js
var utils = require("./utils")
//utils.js
var utils = {
num_PI: function() {return 3.14;}
};
console.log("load utiles");
module.exports = utils;
2、this
//函数对象.call(实例,参数),this等同传递的实例
function test1(a, b) {
this.a = a;
this.b = b;
}
var yk = {};
test1.call(yk, "y", "k");
console.log(yk);//output: {a:"y", b:"k"}
//隐式传递this
var yy = {
name = "y",
y_func:function(){
console.log(this);
}
}
yy.y_func();//将表yy,传递进函数y_func(),this指向yy
//强制传递this
var yk2 = {a:"y"};
var bfunc = function() {
console.log(this);
}.bind(yk2);
bfunc(); //
//bfunc.bind(yk2);----this指向?
//强制bind>显式>隐式传递
//this类似对象实例
function book(name, p){
this.name = name;
this.p = p;
}
var manong = new book("码农", 20);
console.log(manong.name); //---->"码农"
//new prototype,每一个函数对象,都会有一个prototype变量,指向对象
//prototype是一个对象,所以可以扩充这个key: value;
function ykp(name, age) {
this.name = name;
this.age = age;
}
ykp.prototype.y_go = function() {
console.log("yk go!");
}
var yk1 = new ykp("yy", 30);
console.log(yk1);
yk1.y_go(); //--->yk1.__proto__.y_go.call(yk1);
//var a={};
//a _proto_: prototype的浅复制
//实例 作为this,传递给后面的函数
//调用这个函数
//__proto__是什么?prototype是什么?
var func_yk = function() {
console.log("func1");
};
func_yk.prototype.test_log = function(){
console.log("funclog");
};
var yk_log = new func_yk();
yk_log.test_log();
yk_log.__proto__.test_log.call(yk_log);//等同于上面一句