如果不是new调用的,this指向全局对象,在浏览器中指向 window。
new调用,指向调用对象本身。
function inherit(subClass, superClass) {
superClass.prototype = Object.create(superClass.prototype);
superClass.prototype.constructor = subClass;
}
写错了 应该为
subClass.prototype = Object.create(superClass.prototype);
subClass.prototype.constructor = subClass;
注意形参
封装的方法 用于 子类继承父类
分析当前环境如浏览器、nodejs...
require 是模块化工具,按照它的写法可以实现模块化; 以后js 新版本支持 模块化,就不需要它了
同求!
老师你有没有男朋友?
开玩笑~
这个课程是我接触讲解最棒的课程之一了... 真的很赞. 老师讲课方式听着也是很儒雅随和.
也可以理解为引用吧,一般情况下不可枚举的对象是不能被引用的
只能回答问题二:!(){}同等于(function(){})
为了封闭局部变量,避免污染全局空间
Object.defineProperties在全局定义了与那几个构造函数同名的属性,这些属性的值就是这几个构造函数,所以可以在全局通过这些属性访问到构造函数
/** * Created by wz on 16/4/27. */ //这样应该可以了 var Detec=(function(global){ return{ cons:function DetectorBase(configs){ if (!this instanceof DetectorBase) { throw new Error('Do not invoke without new.') } this.configs=configs; this.analyze(); } } })(this); Detec.cons.prototype.analyze=function(){ console.log('Analyzing...'); this.data="##data##"; }; Detec.cons.prototype.detect=function () { throw new Error('Not implemented'); }; function LinkDetector(links){ if (!this instanceof LinkDetector) { throw new Error('Do not invoke without new.') } this.links=links; Detec.cons.apply(this,arguments); } function ContainerDetector(containers){ if (!this instanceof ContainerDetector) { throw new Error('Do not invoke without new.') } this.containers=containers; Detec.cons.apply(this,arguments); } function inherit(subClass,superClass){ subClass.prototype=Object.create(superClass.prototype); subClass.prototype.cons=subClass; } inherit(LinkDetector,Detec.cons); inherit(ContainerDetector,Detec.cons); LinkDetector.prototype.detect=function(){ //console.log(this); //alert(this instanceof LinkDetector); console.log('Loading data:'+this.data); console.log('link detection started.'); console.log('Scaning link:'+this.links); }; ContainerDetector.prototype.detect=function(){ console.log('Loading data:'+this.data); console.log('Container detection started.'); console.log('Scaning containers:'+this.containers); }; Object.defineProperties(this,{ LinkDetector:{value:LinkDetector}, ContainerDetector:{value:ContainerDetector}, DetectorBase:{value:Detec.cons} }); var a=new ContainerDetector('#abc'); var b=new LinkDetector('http://www.baidu.com'); a.detect(); b.detect();
你的第一个匿名函数的最后一个括号放错位置了,而且还同时没有把实参this,也就是window对象传进去,自然执行this.analyze()方法的时候会报错,因为window对象下根本就没有analyze()函数;
肯定不等价的,第一种是调用DetectorBase函数,并且把DetectorBase的作用对象设置为this所指定的对象,并且把相关的参数也传给DetectorBase函数,第二种仅仅是把DetectorBase.prototype对象上的属性和方法继承给LinkDectector.prototype
def just_for_test(a, b):
print
return a & b
在第二页ppt的右边
function inherit(subClass, superClass){
subClass.prototype = Object.create(superClass.prototype);
subClass.prototype.constructor = subClass;
}
}(this);
我觉得应该是编程思想。让项目更加条理规范,方便扩展。比如你网站写js的时候,类似功能间可以通过继承,来模拟,减少代码量,后期还能扩展那不是很酷。还有就是可以加深对JS底层实现的理解,而不是单纯的使用者。:) 共勉~~
sublime哇,不过其实我自己编程用VIM