- +function(exports){
- //命名空间
- var MML={};
- /**
- * 简单继承的实现
- *
- * 1、通过代理function实例对象区分父对象与子对象的prototype属性指向的prototype对象
- * 2、通过闭包原理实现动态生成私有属性存取器
- * 3、子类可以添加自身的私有属性、特权方法、原型对象
- *
- * @time 2015年2月5日13:32:05
- * @author 710335997@qq.com
- *
- * @param {Object} parent
- * @param {Object} param
- */
- MML.extend=function(parent,param){
- function Proxy(){}
- Proxy.prototype=parent.prototype;
- function Son(){
- var _self={};//存储私有属性、真正的隐藏私有属性
- parent.call(this);//拷贝父类属性到子类
- if(param&&typeof param ==="object"){
- for(var p in param){
- var _=p.toString().charAt(0).toUpperCase()+p.toString().substr(1);
- if(p!=="prototype"&¶m.hasOwnProperty(p)&& typeof param[p] ==="string"){
- Son.prototype["set"+_]=(function(key){
- var __=key;
- return function(value){
- _self[__]=value;
- };
- }(p));
- Son.prototype["set"+_](param[p]);
- Son.prototype["get"+_]=(function(key){
- var __=key;
- return function(){
- return _self[__];
- }
- }(p));
- }
- if(p=="prototype" && param[p] && typeof param[p]==="object"){
- for(var a in param[p]){
- Son.prototype[a]=param[p][a];
- }
- }
- }
- }
- };
- Son.prototype=new Proxy();
- return Son;
- };
- exports.MML=MML;
- }(window);
- function Animate(){
- this.type="";
- this.age=3;
- }
- var Dog=new MML.extend(Animate,{
- name:"dog",
- type:"Dog",
- prototype:{
- say:function(){
- console.log("i am "+this.getName());
- }
- }
- });
- var animate=new Animate();
- var dog=new Dog();
- console.log(dog.getName())//输出: dog
- Animate.prototype.test=function(){
- console.log("父类原型方法");
- };
- console.log(Animate.prototype==Dog.prototype);//false 分开了原型
- console.log(animate.test==dog.test);// true 依然可以继续原型方法
- dog.test();//输出: 父类原型方法
- console.log(animate instanceof Animate); // true
- console.log(animate instanceof Dog); //false
- console.log(dog instanceof Dog); // true
- console.log(dog instanceof Animate); //true
- //该片段来自于http://www.codesnippet.cn/detail/2503201511976.html
来源: http://www.codesnippet.cn/detail/2503201511976.html