JGP 読書会1回目

The Good Parts 読書会へのfeedback!

デモ

関数オブジェクト

var inoki = 
	{
		one  : "イチ",
		two  : "ニー",
		three: "サン",
		
		call: function( n )
		{
			if ( typeof n != "number" )	n = 1;
			for ( i = 0; i < n; i++ )
			{
				alert ( this.one + "! " + this.two + "! " + this.three + "! " + "ダー!!" );
			}
		}
	};




function Human( name, age ) 
{	
	this.getName = function()
	{
		return name;
	};
	this.getAge = function()
	{
		return age;
	};
}

var shiraishi = new Human( "kamiyam", 32 );
alert(shiraishi.getName() + "は" + shiraishi.getAge() + "歳");

プロトタイプ

//createの実装確認
if ( typeof Object.create !== 'function' )
{
	//create関数の実装
	Object.create = function(o)
	{
		//空のオブジェクトを生成
		var F = function(){};

		//Fのprototype参照は引数oとする。
		F.prototype = o;

		//オブジェクトFを返す。
		return new F();
	};
}

//carオブジェクトオブジェクトを生成
var car =
{
	type : "CAR",
	name : "Corolla",
	engine : "Normal",
	fuel : 100,
	maxSpeed : 180
};

//sportCarオブジェクトを生成
var sportCar = Object.create(car);

//sportCarオブジェクトのプロパティに値をセット
sportCar.name = "RX-7";
sportCar.engine = "Rotary";
sportCar.fuel = 50;
sportCar.maxSpeed = 250;
sportCar.wing = "GTwing";

alert( car.maxSpeed );		//① car.maxSpeed → 180
alert( car.wing );			//② car.wing → undefined

alert( sportCar.engine );		//③ sportCar.engjine → "Rotary "
alert( sportCar.type );		//④ sportCar.type → car.type → "CAR"
alert( sportCar.turbo );		//⑤ sportCar.turbo → car.turbo → undefined

グローバル変数

var hoge = "fuga";

alert (window["hoge"] );	//fuga