<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>@blog.justoneplanet.info &#187; クラス</title>
	<atom:link href="http://blog.justoneplanet.info/category/computer-language/javascript/class/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.justoneplanet.info</link>
	<description>日々勉強</description>
	<lastBuildDate>Wed, 08 Feb 2012 02:57:17 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>JavaScript Complex Class（複素数のクラス）</title>
		<link>http://blog.justoneplanet.info/2009/09/23/javascript-complex-class%ef%bc%88%e8%a4%87%e7%b4%a0%e6%95%b0%e3%81%ae%e3%82%af%e3%83%a9%e3%82%b9%ef%bc%89/</link>
		<comments>http://blog.justoneplanet.info/2009/09/23/javascript-complex-class%ef%bc%88%e8%a4%87%e7%b4%a0%e6%95%b0%e3%81%ae%e3%82%af%e3%83%a9%e3%82%b9%ef%bc%89/#comments</comments>
		<pubDate>Wed, 23 Sep 2009 09:23:07 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[クラス]]></category>

		<guid isPermaLink="false">http://blog.justoneplanet.info/?p=1936</guid>
		<description><![CDATA[以下のように複素数のクラスを定義することができる。 function Complex(real, imaginary){ this.x = real; this.y = imaginary; } Complex.prot [...]]]></description>
			<content:encoded><![CDATA[<p>以下のように複素数のクラスを定義することができる。</p>
<pre class="brush: jscript;">
function Complex(real, imaginary){
    this.x = real;
    this.y = imaginary;
}
Complex.prototype.magnitude = function(){
    return Math.sqrt(this.x * this.x + this.y * this.y);
}
Complex.prototype.negative = function(){
    return new Complex(-this.x, -this.y);
}
Complex.prototype.add = function(cmp){
    return new Complex(this.x + cmp.x, this.y + cmp.y);
}
Complex.prototype.multiply = function(cmp){
    return new Complex(this.x * cmp.x - (this.y * that.y), this.x * cmp.y + this.y + cmp.x);
}
Complex.prototype.toString = function(){
    return '{' + this.x + ', ' + this.y + '}';
}
Complex.prototype.equals = function(cmp){
    return this.x === cmp.x &amp;&amp; this.y === cmp.y;
}
Complex.prototype.valueOf = function(){
    return this.x;
}
Complex.sum = function(a, b){
    if(a instanceof Complex &amp;&amp; b instanceof Complex){
        return new Complex(a.x + b.x, a.y + b.y);
    }
    else{
        return new TypeError();
    }
}
Complex.product = function(a, b){
    if(a instanceof Complex &amp;&amp; b instanceof Complex){
        return new Complex(a.x * b.x - (a.y * b.y), a.x * b.y + a.y * b.x);
    }
    else{
        return new TypeError();
    }
}
</pre>
<pre class="brush: jscript;">
var a = new Complex(1, 5);//1+5i
alert(a);//{1, 5}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.justoneplanet.info/2009/09/23/javascript-complex-class%ef%bc%88%e8%a4%87%e7%b4%a0%e6%95%b0%e3%81%ae%e3%82%af%e3%83%a9%e3%82%b9%ef%bc%89/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript Class Basic（クラスの基本）</title>
		<link>http://blog.justoneplanet.info/2008/09/23/javascript-class-basic%ef%bc%88%e3%82%af%e3%83%a9%e3%82%b9%e3%81%ae%e5%9f%ba%e6%9c%ac%ef%bc%89/</link>
		<comments>http://blog.justoneplanet.info/2008/09/23/javascript-class-basic%ef%bc%88%e3%82%af%e3%83%a9%e3%82%b9%e3%81%ae%e5%9f%ba%e6%9c%ac%ef%bc%89/#comments</comments>
		<pubDate>Tue, 23 Sep 2008 08:02:58 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[クラス]]></category>

		<guid isPermaLink="false">http://blog.justoneplanet.info/?p=1931</guid>
		<description><![CDATA[JavaScriptには現時点でclassという概念がない（JavaScript2.0から使える予定）。 ■メンバ インスタンスプロパティ インスタンス化した後のオブジェクトのプロパティ。 function Person [...]]]></description>
			<content:encoded><![CDATA[<p>JavaScriptには現時点でclassという概念がない（JavaScript2.0から使える予定）。</p>
<h3>■メンバ</h3>
<h4>インスタンスプロパティ</h4>
<p>インスタンス化した後のオブジェクトのプロパティ。</p>
<pre class="brush: jscript;">
function Person(name){
    this.name = name;
    this.introduce = function(){
        alert('My name is ' + this.name + '.');
    };
}
var john = new Person('John');
//an instance property is john.name
</pre>
<h4>インスタンスメソッド</h4>
<p>インスタンス化した後のオブジェクトのメソッド。</p>
<pre class="brush: jscript;">
function Person(name){
    this.name = name;
    this.introduce = function(){
        alert('My name is ' + this.name + '.');
    };
}
var john = new Person('John');
//an instance property is john.introduce()
</pre>
<p>但し、以下のようにprototypeによってインスタンスメソッドを定義した場合、introduce()メソッドのnameにはthisが必須となる。</p>
<pre class="brush: jscript;">
var undefined;
function Person(name){
    this.name = name;
}
Person.prototype.introduce = function(){
    alert(this.name);
}
var john = new Person('John');
john.introduce();
</pre>
<h4>クラスプロパティ</h4>
<p>以下のようにすると、クラスプロパティが定義できる。インスタンス化されたオブジェクトからは当然アクセスできない。クラスプロパティは（クラス定数のように）慣例で全て大文字にする。</p>
<pre class="brush: jscript;">
function Person(name){
    this.name = name;
    this.introduce = function(){
        alert('My name is ' + this.name + '.');
    };
}
Person.GENUS = 'genus Homo';
var john = new Person('John');
alert(john.GENUS);//none
alert(Person.GENUS);//genus Homo
</pre>
<h4>クラスメソッド</h4>
<p>クラスメソッドには以下のようなものがある。PHPで言い換えるならばstaticをつけてクラスメソッドを宣言したような感じだ。自身で定義したクラスにメソッドを付加することも可能である。</p>
<pre class="brush: jscript;">
alert(Date.parse('2009/09/23 17:30:37'));//1253694637000
</pre>
<h3>■プライベートメンバ</h3>
<p>JavaやPHPではアクセス修飾詞を使用してコントロールする機能。但し、以下のようにプロパティに保持しないというだけであり、グローバル空間からインスタンス化したオブジェクトを介してnameを表示させることはできる。メリットはnameを外から変更できない事だ。</p>
<pre class="brush: jscript;">
function Person(name){
    this.getName = function(){return name;};
}
Person.prototype.introduce = function(){
    alert(this.getName());
}
var john = new Person('John');
alert(john.getName());//John
john.introduce();//John
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.justoneplanet.info/2008/09/23/javascript-class-basic%ef%bc%88%e3%82%af%e3%83%a9%e3%82%b9%e3%81%ae%e5%9f%ba%e6%9c%ac%ef%bc%89/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript Constructor and Prototype</title>
		<link>http://blog.justoneplanet.info/2008/09/21/javascript-constructor-and-prototype/</link>
		<comments>http://blog.justoneplanet.info/2008/09/21/javascript-constructor-and-prototype/#comments</comments>
		<pubDate>Sun, 21 Sep 2008 05:05:40 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[クラス]]></category>

		<guid isPermaLink="false">http://blog.justoneplanet.info/?p=1911</guid>
		<description><![CDATA[■コンストラクタ関数 以下のようにするとコンストラクタ関数を定義できる。 function Person(name){ this.name = name; this.introduce = function(){ ale [...]]]></description>
			<content:encoded><![CDATA[<h3>■コンストラクタ関数</h3>
<p>以下のようにするとコンストラクタ関数を定義できる。</p>
<pre class="brush: jscript;">
function Person(name){
    this.name = name;
    this.introduce = function(){
        alert(&quot;My name is &quot; + this.name + &quot;.&quot;);
    }
}
var john = new Person('John');
var mike = new Person('Mike');
john.introduce();//My name is John.
mike.introduce();//My name is Mike.
</pre>
<p>慣例でコンストラクタ関数（クラス）は大文字で定義する。</p>
<h3>■プロトタイプ</h3>
<p>上述のコードは全てのインスタンスが同じintroduce()メソッドを保持するようになる。もし全てのインスタンスが同じintroduce()メソッドを参照すればメモリの節約につながる。そこで以下のようにprototypeを用いる。</p>
<pre class="brush: jscript;">
function Person(name){
    this.name = name;
}
Person.prototype.introduce = function(){
    alert(&quot;My name is &quot; + this.name + &quot;.&quot;);
}
var john = new Person('John');
var mike = new Person('Mike');
john.introduce();//My name is John.
mike.introduce();//My name is Mike.
</pre>
<p>この場合、以下のように書くとメソッドが増えたときにスッキリする。</p>
<pre class="brush: jscript;">
function Person(name){
    this.name = name;
}
Person.prototype = {
    introduce : function(){
        alert(&quot;My name is &quot; + this.name + &quot;.&quot;);
    }
}
var john = new Person('John');
var mike = new Person('Mike');
john.introduce();//My name is John.
mike.introduce();//My name is Mike.
</pre>
<div class="kakomi">
<h4>参考</h4>
<p>以下のようにしてArrayクラスを継承させることもできる。</p>
<pre class="brush: jscript;">
var undefined;
function Stack(){
    this.T = -1;
}
Stack.prototype = Array.prototype;
</pre>
</div>
<h3>■プロパティの性質</h3>
<p>以下のようにコンストラクタ関数で記述したintroduceメソッドは、直属のプロパティ（メソッド）になる。</p>
<pre class="brush: jscript;">
function Person(name){
    this.name = name;
    this.introduce = function(){
        alert(&quot;My name is &quot; + this.name + &quot;.&quot;);
    }
}
var john = new Person('John');
alert(john.hasOwnProperty('name'));//true
alert(john.hasOwnProperty('introduce'));//true
alert(&quot;introduce&quot; in john);//true
</pre>
<p>一方で以下のようにprototypeを使用した場合、introduceメソッドはPersonから継承されたプロパティ（メソッド）ということになる。</p>
<pre class="brush: jscript;">
function Person(name){
    this.name = name;
}
Person.prototype = {
    introduce : function(){
        alert(&quot;My name is &quot; + this.name + &quot;.&quot;);
    }
}
var john = new Person('John');
alert(john.hasOwnProperty('name'));//true
alert(john.hasOwnProperty('introduce'));//false
alert(&quot;introduce&quot; in john);//true
</pre>
<h3>■prototypeにおける読み込みと書き込み</h3>
<h4>読み込み</h4>
<p>プロパティを読み込む場合、インスタンスはプロトタイプのプロパティを参照するので（継承している）、prototypeが使われる。</p>
<pre class="brush: jscript;">
function Person(name){
    this.name = name;
}
Person.prototype = {
    introduce : function(){
        alert(&quot;My name is &quot; + this.name + &quot;.&quot;);
    }
}
var john = new Person('John');
var mike = new Person('Mike');
john.introduce();//My name is John.
mike.introduce();//My name is Mike.
</pre>
<p>インスタンスは自身にプロパティが見つからない場合、コンストラクタ関数のprototypeプロパティを参照する。コンストラクタ関数で見つからない場合は、さらにprototypeをたどる。</p>
<h4>書き込み</h4>
<p>プロパティを書き込む場合、JavaScriptはprototypeを使用しない。</p>
<pre class="brush: jscript;">
function Person(name){
    this.name = name;
}
Person.prototype = {
    introduce : function(){
        alert(&quot;My name is &quot; + this.name + &quot;.&quot;);
    }
}
var john = new Person('John');
var mike = new Person('Mike');
john.introduce = function(){
    alert(&quot;I'm &quot; + this.name + &quot;.&quot;);
}
john.introduce();//I'm John.
mike.introduce();//My name is Mike.
</pre>
<h3>■ネイティブコンストラクタ関数の拡張</h3>
<p>通常は行ってはならないが、古いブラウザに対して標準に準拠したメソッドを付加するには便利である。</p>
<h4>array.forEach(callback[, thisObj]);</h4>
<p>配列の各要素を引数にして指定された関数を呼び出す。</p>
<pre class="brush: jscript;">
if(!Array.prototype.forEach){
    Array.prototype.forEach = function(func/*, thisObj*/){
        var length = this.length &gt;&gt;&gt; 0;
        if(typeof func !== 'function'){
            throw new TypeError();
        }
        var thisObj = arguments[1];
        for(var i = 0; i &lt; length; i++){
            if(i in this){
                func.call(thisObj, this[i], i, this);
            }
        }
    }
}
</pre>
<h5>サンプル</h5>
<pre class="brush: jscript;">
[1, 2, 3, 4].forEach(
	function(element, index, array){
		alert(&quot;[&quot; + index + &quot;] is &quot; + element);
	}
);
/*
[0] is 1
[1] is 2
[2] is 3
[3] is 4
*/
</pre>
<h4>array.map(callback[, thisObj]);</h4>
<p>配列の各要素（を引数として）に対して関数を実行し、返された値を配列にまとめて返す。</p>
<pre class="brush: jscript;">
if(!Array.prototype.map){
    Array.prototype.map = function(func/*, thisObj*/){
        var length = this.length &gt;&gt;&gt; 0;
        if(typeof func !== 'function'){
            throw new TypeError();
        }
        res = new Array(length);
        var thisObj = arguments[1];
        for(var i = 0; i &lt; length; i++){
            if(i in this){
                res[i] = func.call(thisObj, this[i], i, this);
            }
        }
        return res;
    }
}
</pre>
<h5>サンプル</h5>
<pre class="brush: jscript;">
alert([1, 16, 81].map(Math.sqrt));//1,4,9
</pre>
<h4>function.apply()</h4>
</p>
<pre class="brush: jscript;">
if(!Function.prototype.apply){
    Function.prototype.apply= function(object, parameters){
        var f = this;
        var o = object || window;
        var args = parameters || [];
        o._$_apply_$_ = f;
        var stringArgs = [];
        for(var i = 0; i &lt; args.length; i++){
            stringArgs[i] = &quot;args[&quot; + i + &quot;]&quot;;
        }
        var arglist = stringArgs.join(&quot;,&quot;);
        var methodcall = &quot;o._$_apply_$_(&quot; + arglist + &quot;);&quot;;
        var result = eval(methodcall);
        delete o._$_apply_$_;
        return result;
    }
}
</pre>
<div class="kakomi">
<h4>各メソッドについて</h4>
<dl>
<dt>array.forEach(callback([element[, index[, array]]])[, thisObj]);</dt>
<dd>callback…各要素を引数として実行される関数。</dd>
<dd>thisObj…callbackが実行するされるときにthisとして扱われるオブジェクト。</dd>
<dt>array.map(callback[, thisObj]);</dt>
<dd>callback…各要素を引数として実行される関数。</dd>
<dd>thisObj…callbackが実行するされるときにthisとして扱われるオブジェクト。</dd>
<dt>var result = fun.apply(thisArg, [argsArray]);</dt>
<dd>thisArg…カレントオブジェクト。省略された場合はグローバルオブジェクト。</dd>
<dd>argsArray…関数に渡される引数の配列。</dd>
</dl>
<h4>参考</h4>
<ul>
<li><a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/forEach">forEach</a></li>
<li><a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/map">map</a></li>
<li><a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Function/apply">apply</a></li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.justoneplanet.info/2008/09/21/javascript-constructor-and-prototype/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

