javascript prototype

414 views
369 views

Published on

javascript prototype

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
414
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
3
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

javascript prototype

  1. 1. S53#22-1 type2-2 prototype2-3 prototype chain2-4 technique2012.10.24 20:30 ~ 22:30
  2. 2. Type = Id = Category = Group var a = {type:1};int memory▼ recognize▼ scalable● var b = {type:2}; var a = {type:"food"};String memory▲ recognize▲ scalable▼ var b = {type:"tool"};Object memory▼ recognize▲ scalable▲ var food = {}; var tool = {};Self(Recusive) - Reference var a = {type:food};var food = {}; var b = {type:tool};var tool = {};alert( food.type );Conclusion = Functionvar food = function(){};var tool = function(){};var a = {type:food};var b = {type:tool};alert( a.type ); // foodalert( food.type ); // undefined
  3. 3. function test(){ Function[test] = { ... ...} prototype:{alert( typeof test.prototype ); constructor:Function[test]alert( test === test.prototype.constructor ); } };var a = new test( 3 ); var a = { __proto__:test.prototypealert( a.constructor === test ); };alert( a instanceof test ); a = test.call( a, 3 ) || a;function test( $val ){ a={ this.val = $val; __proto__:test.prototype,} val:3 };function test( $val ){ a = {}; this.val = $val; return {};}
  4. 4. function test1(){ var a = new test1;}function test2(){ alert( a.constructor === test1 );} alert( a instanceof test1 );test1.prototype.constructor = test2;alert( test2 === test1.prototype.constructor );test1.prototype = test2.prototype; function test(){alert( test2 === test1.prototype.constructor ); } test.prototype.val = 3;function test(){} var a = new test, b = new test;test.prototype.action = function(){}; alert( a.val );var a = new test, b = new test; a.val = 5;alert( a.action === b.action ); alert( b.val );
  5. 5. function parent(){ a={} __proto__:child.prototypeparent.prototype.add = function(){}; };function child(){ //1. a[???]} //2. a.__proto__[???]child.prototype.action = function(){}; a.add === undefinedvar a = new child;a.add();Goal = a.__proto__.__proto__.add();1. a.__proto__ = child.prototype;2. a.__proto__.__proto__ = child.prototype.__proto__ = parent.prototype;3. child.prototype = new parent;4. a.__proto__.__proto__.add = parent.prototype.add;
  6. 6. function parent(){}parent.prototype.add = function(){};parent.prototype.remove = function(){};...function child( a, b ){ this.a = a; this.b = b;}child.prototype = new parent;child.prototype.action1 = function(){};child.prototype.action2 = function(){};...var a = new child;a.a; // a.aa.b; // a.ba.action1(); // a.__proto__.action1a.action2(); // a.__proto__.action2a.add(); // a.__proto__.__proto__.adda.remove(); // a.__proto__.__proto__.remove
  7. 7. function test( a, b ){ function test( a, b ){ if( this == window ){ ... return new test( a, b ); } }else{ this.a = a; test.prototype.action1 = function(){}; this.b = b; test.prototype.action2 = function(){}; }} test.prototype = { constructor: test.prototype.constructor,var a = test( 3, 5 ); action1: function(){},var b = new test( 3, 5 ); action2: function(){} };alert( a.constructor === b.constructor );function parent( a ){ this.a = a;}function child( a, b ){ this.super( a ); this.b = b;}child.prototype = new parent;child.prototype.super = parent;var a = new child( 3, 6 );alert( a.a );

×