// 闭包用来传递属性,继承关系 var deferred; // ... functiondef(context, klassName) { klassName || (klassName = context, context = global); // 定义一个构造函数 var Klass = context[klassName] = functionKlass(){ if(context != this){ // called as a constructor // allow init to return a different class/object returnthis.init && this.init.apply(this, arguments); } // called as a function - defer setup of superclass and properties deferred._super = Klass; // 继承 deferred._props = arguments[0] || { }; // 传递属性 } // ... } global.def = def; }(this));
当把Person这个构造函数当做普通函数使用的时候,context 和 this 都是 global,会利用Person和Ninja共同的闭包deferred做属性传递。
当示例代码执行到 N << temp 的时候,JS 会自动调用 def.js 内定义的valueOf方法,实现继承。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
deferred.valueOf = function(){ var Superclass = deferred._super; // inherit from superclass Subclass.prototype = Superclass.prototype; var proto = Klass.prototype = new Subclass; // reference base and superclass Klass._class = Klass; Klass._super = Superclass; // enforce the constructor to be what we expect proto.constructor = Klass; // to call original methods in the superclass proto._super = base; // set properties deferred(deferred._props); };
目标的方向。不是所有的目标都能带来长久地快乐。目标是不是满足人的基本需求,关联感(帮助别人、建立人际,回报社会),胜任力(提高自身能力,经验)和自主权(自己的追求是动力的源泉)。- 为什么 or 是什么。 “为什么”是抽象的,有助于提高抗诱惑力,让人冲劲满满。“是什么”是具体的,有助于提高行动力,让人专注具体操作。