javascript object orented programming(三)

2448 ワード

/*

   *     ,            

   *           ,      ,            "  "。

   * json      Douglas Crockford,     object()  ,       。

  */

   var chinese = {

		nation:"  "

	}



	var Doctor = {

		career:'  '

	}

	//

	function object(o){

		//          F,F prototype o(   ,          )     ,

		//    F.prototype         

		function F(){}

		F.prototype = o;

		//  F          ,  F                 。

		return new F();

	}



	var Doctor = object(chinese);

	Doctor.career = '  ';       //  ,           :           !

	console.log(Doctor.nation)	  //                      。



	  /*

		*  、    (         )    jQuery       。

		*         "prototype "  ,       :       ,        ,      。

	  */

		var chinese = {

			nation:"  ",

			birthPlaces :['  ','  ','  ']

		}

		var Doctor = {

			career:'  '

		}

		function extendCopy(p){

			var c = {}				//       ,          

			for(var i in p){

				c[i] = p[i];

			}

			c.uber = p;				//               ,    ~

			return c;

		}

		var Doctor = extendCopy(chinese);

		//     Doctor.birthPlaces     ,chinese.birthPlaces      

		Doctor.birthPlaces.push('  ');

		console.log(Doctor.nation);



		console.log(Doctor.birthPlaces);

		console.log(chinese.birthPlaces);

	 /*

	  *  、      ,jQuery            。

	  *	  "   ",                    。       ,      "   "   。

	 */

	 var chinese = {

    nation:"  ",

    birthPlaces :['  ','  ','  ']

	}

	var Doctor = {

		career:'  '

	}

	function deepCopy(p,c){

		var c = c || {};

		for(var i in p){

			if(typeof p[i] ==='object'){

				c[i] = (p[i].constructor === Array) ? [] :{};

				deepCopy(p[i],c[i]);

			}else{

				c[i] = p[i];

			}

		}

	 return c;

	}

	var Doctor = deepCopy(chinese);

	//  ,         ,    。  ,           :



	Doctor.birthPlaces.push('  ');

	console.log(Doctor.nation);

	console.log(Doctor.birthPlaces); //["  ", "  ", "  ", "  "]

	//  ,           。

	console.log(chinese.birthPlaces); //["  ", "  ", "  "]

非構造関数の継承
メモを学び、学習過程を記録するところ.
リンク:http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_イノセンスcontinued.