javascript 的 OO

/** * 封装 */ 

function cl( mess){ console.log( mess); } // // 类 , 对象 ; 类型 , 变量 // JavaScript 中 类 是通过 function 关键字来定义的 function User( Name, Age){ var age = Age; // 私有,通过 对象 不能访问 this.name = Name; // 共有,通过 对象 可以访问 // var say = function(){ alert( "are you ok?"); }; var sayHello = function(){ alert( "hello world"); say(); // 合法,私有方法可以调用私有方法,但是私有方法不能调用共有方法 // this.say(); //不合法, 因为 this 在这指 window 对象。通过下面查看 // alert( this); }; this.say = function(){ alert( "how are you?"); } // this.getName = function(){ // 共有,其实没必要,因为 this.name 是共有,直接通过 对象 可以访问 return this.name; }; // this.getAge = function(){ // 特权,通过 对象 可以访问 sayHello(); // 合法 return age; }; this.setAge = function( Age){ // 特权,通过 对象 可以访问,可以访问 对象 的私有属性 age = Age; }; } // 通过 类名 向类中增加 共有方法 和 共有属性,这种方法增加的成员在所有该类的(包括已经创建的)对象中生效,但是不能覆盖; // 若 类 定义中声明了同名的 共有成员, 则通过这种方法增加的成员不会覆盖原来的成员 // 注意是向 =类= 中增加 // 下面两个都会生效 User.prototype.healthy = false; User.prototype.isFine = function(){ return this.healthy ? "I am fine!! Thanks" : "I am not fine!"; }; // var me = new User( 'zhanglin', 23); for( var name in me){ cl( name + ":::::" + me[name]); } for( var name in me){ // hasOwnProperty() 会过滤掉通过 prototype 原型增加的成员 if( me.hasOwnProperty( name) ){ cl( name + ":::::" + me[name]); } } // me.money = 100; // 增加 =对象= 的属性(共有),与 类 没有关系 me.growOld = function(){ var oldAge = this.getAge(); // sayHello(); 不合法,在 =类= 外不能调用 私有 方法 this.setAge( oldAge + 1); } me.growOld(); // // 虽然在 类 定义中没有这两个成员,但是在 me 中增加了这两个成员,因此不会覆盖 me 中的这两个成员 // 但是用 类 创建 新对象时,这两个成员都会在新对象中出现,见 me2 User.prototype.money = 200; User.prototype.growOld = function(){ // alert( "new"); }; // cl( ' '); for( var name in me){ cl( name + ":::::" + me[name]); } // User.prototype.money = 200; User.prototype.growOld = function(){ alert( "OK"); }; // // 下面的不会生效,因为类定义中已经有了 User.prototype.name = "myhere"; User.prototype.getAge = function(){ return 100; } // var me2 = new User( 'girl', 1000); // cl(" "); for( var name in me2){ cl( name + ":::::" + me2[name]); } me2.growOld();

 

// // 每个通过 {} 创建的对象的原型都是 Object.prototype。 // /* 对象 A 的原型是 B 1, update 和 delete A 的属性不会影响 B 的属性;即 update 或 delete A 的属性时,只是操作 A,与 B 没有关系 2, retrieve A 的属性时,有可能会得到 B 的属性;即,如果属性在 A 中不存在就回去 B 中找, 如果 B 中不存在回去 B 的原型找,直到到达 Object.prototype, 如果还没有返回 undefined 总之: 只有在获取对象属性时才可能与他的原型有关系!! */ 

 

/** * this 的理解 * * 非常重要的一点: * this 是动态绑定的,给 this 赋值实在 函数被调用的时候 */ // // Functions in JavaScript are objects. // 函数在 js 中有四种调用方式。 // 1, method 方式 var obj = { value: 1, increment: function(){ this.value += 1; } }; obj.increment(); // // 2, constructor 方式 function User(){ var age; this.name; this.setAge = function( a){ age = a; }; this.getAge = function(){ return age; } } var me = new User(); // 3, function 方式 var add = function( a, b){ return a + b; } add( 1, 2) // 4, apply 方式 var obj = { getValue: function(){ return this.value; } }; var obj_2 = { value: 'myhere' } var v = obj.getValue.apply( obj_2) // // 四种调用方式中 this 的含义不同,主要记录下 第三种 方式( function 调用) 中的 this // 要查看 this 指哪个对象,直接 alert( this) 就可以 // func_name() 方式中 this 指向 global 对象,通常是 window // // function 调用方式的 this var add = function( a, b){ return a + b; }; Function.prototype.method = function( name, func){ this.prototype[ name] = func; }; Function.method( 'a1', function(){ var slice = Array.prototype.slice, args = slice.apply( arguments); // alert( this) // 为 window 对象 return function(){ // alert( this) // 为 函数对象( 函数的 native code) return this.apply( null, args.concat( slice.apply( arguments))); } }( 1)); // 这里 method 的第二个参数是一个表达式,这个表达式立即执行了。 Function.method( 'a2', function(){ var slice = Array.prototype.slice, args = slice.apply( arguments), that = this; // this 为动态绑定 return function(){ return that.apply( null, args.concat( slice.apply( arguments))); } }); // 形成 closure,this 要想使 外层函数的 this 在内层函数中可见,必须将外层函数的 this 赋给一个新的变量 var a = add.a1( 2); // a = 3 // 返回的是一个函数运行的结果 var b = add.a2( 4); // 返回的是一个函数 var c = b( 1); // b = 5 /** * this 是动态绑定的,this 赋值是在函数调用的时候 */  

你可能感兴趣的:(javascript 的 OO)