JavaScriptは対象二に向ける.

10146 ワード

 、   :

1、 prototype , ,prototype constructor , prototype ;
2、prototype prototype , prototype , constructor Object ;
3、 __proto__, , ( prototype , )
4、 , , , prototype , ;

、 :

isPrototypeOf:  
hasOwnProperty:
in:             , true,
for-in:         for , ( ie6 bug, [[DontEnum]] , , )

、 :

1、
function obj(arg1,arg2){

	var o = new Object();

	o.arg1 = arg1;

	o.arg2 = arg2;

	o.method = function(){

		console.log(arg1+arg2);

	}

	return o;

}

var obj1 = obj(10,20);

obj1.method();	//30




2、
function Obj(arg1,arg2){

	this.arg1 = arg1;

	this.arg2 = arg2;

	this.method = function(){

		console.log(arg1+arg2);

	}

}

var obj1 = new Obj(10,20);

obj1.method();	//30




3、
function Obj(){}

Obj.prototype = {

	constructor: Obj,

	arg3: ["a","b","c"],

	method: function(){

		console.log(this.arg3.toString());

	}

}

var obj1 = new Obj();

obj1.method();	//a,b,c

var obj2 = new Obj();

obj2.arg3.push("d");

obj1.method();	//a,b,c,d

  :             ,                          


4、
function Obj(arg1,arg2){

	this.arg1 = arg1;

	this.arg2 = arg2;

	this.arg3 = ["a","b","c"];

}

Obj.prototype = {

	constructor: Obj,

	method: function(){

		console.log(this.arg1+this.arg2);

	}

}

var obj1 = new Obj(10,20);

obj1.method();  //30

var obj2 = new Obj();

obj2.arg3.push("d");

console.log(obj1.arg3.toString());	//a,b,c

  :       ,        


5、
function Obj(arg1,arg2){

	this.arg1 = arg1;

	this.arg2 = arg2;

	if(typeof this.method != "function"){

		Obj.prototype.method = function(){

			console.log(this.arg1+this.arg2);

		}

	}

}

var obj1 = new Obj(10,20);

obj1.method();  //30

  :              ,               


6、
function Obj(arg1,arg2){

	var o = new Object();

	o.arg1 = arg1;

	o.arg2 = arg2;

	o.method = function(){

		console.log(this.arg1+this.arg2);

	}

	return o;

}

var obj1 = new Obj(10,20);

obj1.method();  //30

  :       ,        


7、
function Obj(arg1){

	var o = new Object();

	o.method = function(){

		console.log(arg1);

	}

	return o;

}

var obj1 = Obj(10);

obj1.method();	//10

  :       method      arg1  , :             


8、
function Super(arg1,arg2){

	this.arg1 = arg1;

	this.arg2 = arg2;

	this.arg3 = ["a","b","c"];

}

Super.prototype = {

	constructor: Super,

	superMethod: function(){

		console.log(this.arg3.toString());

	}	

}

function Sub(){

	

}

Sub.prototype = new Super();

Sub.prototype.constructor = Sub;



var obj1 = new Sub();

obj1.superMethod();  //a,b,c

var obj2 = new Sub();

obj2.arg3.push("d");

obj1.superMethod();  //a,b,c,d

  :                ,                   


9、
function Super(){

	this.arg3 = ["a","b","c"];

}

function Sub(){

	Super.call(this);

}

var superObj = new Super();

superObj.arg3.push("d");

var obj1 = new Sub();

console.log(obj1.arg3.toString());	//a,b,c

  :                 , :                      


10、
function Super(arg1,arg2){

	this.arg1 = arg1;

	this.arg2 = arg2;

	this.arg3 = ["a","b","c"];

}

Super.prototype = {

	constructor: Super,

	superMethod: function(){

		console.log(this.arg3.toString());

	}	

}

function Sub(){

	Super.call(this,10,20);  //       ,           

}

Sub.prototype = new Super();  //           

var obj1 = new Sub();

obj1.superMethod();	 //a,b,c

var obj2 = new Sub();  //           

obj2.arg3.push("d");

obj1.superMethod();	 //a,b,c

  :                      ,     , :           ,            


11、
function object(o){

	function F(){}

	F.prototype = o;

	return new F();

}

var o = {

	arg1: 10,

	method: function(){

		console.log(this.arg1);	

	}

}

var obj1 = object(o);

obj1.method();

  :  object         o          


12、
function object(o){

	function F(){}

	F.prototype = o;

	return new F();

}

var o = {

	arg1: 10,

	method: function(){

		console.log(this.arg1);	

	}

}

function createAnother(original){

	var clone = object(original);

	clone.sayHi = function(){

		alert("hi");	

	}

	return clone;

}

var obj1 = createAnother(o);

obj1.sayHi();	//hi

  :          ,                 


13、
function object(o){

	function F(){}

	F.prototype = o;

	return new F();

}

function inheritPrototype(subType,superType){

	var prototype = object(superType.prototype);	//   superType.prototype          ,                      

	prototype.constructor = subType;				// prototype         constructor   

	subType.prototype = prototype;					//    prototype         ,                  ( :           prototype  )         

}

function Super(arg1,arg2){

	this.arg1 = arg1;

	this.arg2 = arg2;

}

Super.prototype = {

	constructor: Super,

	superMethod: function(){

		console.log(this.arg1+this.arg2);

	}	

}

function Sub(arg1,arg2){

	Super.call(this,arg1,arg2);

}

inheritPrototype(Sub,Super);

var obj1 = new Sub(10,20);

obj1.superMethod();	 //30

console.log(obj1.constructor);	//Sub(arg1,arg2){ xxx }



var superObj = new Super();

console.log(superObj.constructor);	//Super(arg1,arg2){ xxx }

  :                    ,        


14、jQuery
var $ = Fn = jQuery = function(){

	return new Fn.prototype.init();

}

Fn.prototype = jQuery.prototype = {

	init: function(){

		return this;

	},

	method1: function(){

		console.log("method1");

	},

	method2: function(){

		console.log("method2");

	}

}

Fn.prototype.init.prototype = Fn.prototype;


     , , jQuery , , 。
     ,jQuery , jQuery init ,init this,
this , init , jQuery ;
     init jQuery , jQuery jQuery 。

	var obj = $();

	obj.method1();	//method1


[ : JavaScript ]