<?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/object/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.justoneplanet.info</link>
	<description>JavaScript、PHP、MySQLを使ったり</description>
	<lastBuildDate>Sun, 25 Jul 2010 07:34:20 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>JavaScript Object</title>
		<link>http://blog.justoneplanet.info/2009/01/18/javascript-object/</link>
		<comments>http://blog.justoneplanet.info/2009/01/18/javascript-object/#comments</comments>
		<pubDate>Sat, 17 Jan 2009 17:34:48 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[オブジェクト]]></category>

		<guid isPermaLink="false">http://blog.justoneplanet.info/?p=1885</guid>
		<description><![CDATA[■オブジェクトを生成
オブジェクトリテラルを使った生成
世間的には一般的である。

var obj = {};
var john = {
    &#34;name&#34; : &#34;John Norton&#038; [...]]]></description>
			<content:encoded><![CDATA[<h3>■オブジェクトを生成</h3>
<h4>オブジェクトリテラルを使った生成</h4>
<p>世間的には一般的である。</p>
<pre class="brush: jscript;">
var obj = {};
var john = {
    &quot;name&quot; : &quot;John Norton&quot;,
    &quot;age&quot; : 28,
    &quot;married&quot; : true
};
</pre>
<h4>new演算子を使った生成</h4>
<p>個人的には以下のようにnew演算子とコンストラクタ関数を定義する方法が好きだ。</p>
<pre class="brush: jscript;">
function Person(name, age, married){
    this.name = name;
    this.age = age;
    this.married = married;
}
var john = new Person('John Norton', 28, true);
</pre>
<p>引数の順番が固定なのが嫌なので、以下の様な方法も個人的には使ったりする。</p>
<pre class="brush: jscript;">
function Person(arg){
    this.name = arg.name;
    this.age = arg.age;
    this.married = arg.married;
}
var john = new Person({
    &quot;name&quot; : &quot;John Norton&quot;,
    &quot;age&quot; : 28,
    &quot;married&quot; : true
});
</pre>
<h3>■オブジェクトのプロパティ</h3>
<pre class="brush: jscript;">
var book = {};
book.title= &quot;桃太郎&quot;;
book.chapter1 = new Object();
book.chapter1.title = &quot;おじいさん芝刈り&quot;;
book.chapter2 = {
    &quot;title&quot; : &quot;おばあさん川へ洗濯&quot;
}
</pre>
<h4>プロパティの調査</h4>
<p>以下のようにするとユーザ定義のプロパティが調べられる。</p>
<pre class="brush: jscript;">
function surveyProperties(obj){
    var txt = '';
    for(var p in obj){
        txt .= p + &quot;\n&quot;;
    }
    alert(txt);
}
</pre>
<p>但し、継承されたプロパティは返されない。</p>
<h5>プロパティの存在確認</h5>
<p>存在しないプロパティを参照するとundefinedが返る。</p>
<pre class="brush: jscript;">
if(john.name !== undefined){
    //code
}
</pre>
<p>以下のようにin演算子を使用しても確認できる。</p>
<pre class="brush: jscript;">
if(&quot;name&quot; in john){
    //code
}
</pre>
<p>プロパティが存在し、undefined（未定義値）が代入されていた場合、上述のコードは異なった結果をもたらす。</p>
<h4>プロパティの削除</h4>
<p>以下のようにdelete演算子を使用して、プロパティを削除する。</p>
<pre class="brush: jscript;">
delete john.name;
</pre>
<h3>■連想配列とオブジェクト</h3>
<p>JavaScriptにおける連想配列はオブジェクトのプロパティである。</p>
<pre class="brush: jscript;">
alert(john.name);//John Norton
alert(john['name']);//John Norton
</pre>
<p>つまりJavaScriptではプロパティの名前を文字列として与えられるので、プログラム中で生成する事が出来る。</p>
<h3>■プロパティ</h3>
<h4>コンストラクタプロパティ</h4>
<p>constructorプロパティにはコンストラクタ関数が格納されている。但し、alertした場合は関数名だけでなく内部のコードも表示される。</p>
<pre class="brush: jscript;">
var date = new Date();
alert(date.constructor === Date);// true
function Person(arg){
    this.name = arg.name;
    this.age = arg.age;
    this.married = arg.married;
}
var john = new Person({
    &quot;name&quot; : &quot;John Norton&quot;,
    &quot;age&quot; : 28,
    &quot;married&quot; : true
});
alert(john.constructor);
/*
function Person(arg) {
    this.name = arg.name;
    this.age = arg.age;
    this.married = arg.married;
}
*/
alert(john.constructor === Person);//true
</pre>
<p>以下のようにtypeof演算子を使用して、変数の型を調べられる。</p>
<pre class="brush: jscript;">
function Person(arg){
    this.name = arg.name;
    this.age = arg.age;
    this.married = arg.married;
}
var john = new Person({
    &quot;name&quot; : &quot;John Norton&quot;,
    &quot;age&quot; : 28,
    &quot;married&quot; : true
});
if(typeof john ===&quot;object&quot;){
    alert('&quot;john&quot; is a Object');
}
</pre>
<p>以下のようにinstanceof演算子を使用して、任意のクラスのインスタンスかどうかテストできる。</p>
<pre class="brush: jscript;">
function Person(arg){
    this.name = arg.name;
    this.age = arg.age;
    this.married = arg.married;
}
var john = new Person({
    &quot;name&quot; : &quot;John Norton&quot;,
    &quot;age&quot; : 28,
    &quot;married&quot; : true
});
if(john instanceof Person){
    alert('&quot;john&quot; is instance of Person');
}
</pre>
<h3>■メソッド</h3>
<h4>hasOwnProperty()メソッド</h4>
<p>引数で指定した名前のプロパティを持つかどうかを返す。但し、継承したものはfalseとなる。</p>
<pre class="brush: jscript;">
function Person(arg){
    this.name = arg.name;
    this.age = arg.age;
    this.married = arg.married;
}
var john = new Person({
    &quot;name&quot; : &quot;John Norton&quot;,
    &quot;age&quot; : 28,
    &quot;married&quot; : true
});
john.hasOwnProperty('toString');//false
john.hasOwnProperty('name');//true
</pre>
<h4>propertyIsEnumerable()メソッド</h4>
<p>hasOwnProperty()メソッドでtrueを返し、さらにfor/inループで列挙が可能なものかどうかを返す。</p>
<pre class="brush: jscript;">
function Person(arg){
    this.name = arg.name;
    this.age = arg.age;
    this.married = arg.married;
}
var john = new Person({
    &quot;name&quot; : &quot;John Norton&quot;,
    &quot;age&quot; : 28,
    &quot;married&quot; : true
});
john.hasOwnProperty('toString');//false
john.hasOwnProperty('name');//true
</pre>
<h4>isPrototypeOf()メソッド</h4>
<p>引数で指定したオブジェクトのprototypeオブジェクトであるかどうかを返す。</p>
<pre class="brush: jscript;">
var john = {};
alert(Object.prototype.isPrototypeOf(john));//true
alert(Object.isPrototypeOf(john));//false
alert(john.isPrototypeOf(Object.prototype));//false
alert(Function.prototype.isPrototypeOf(Object));//true
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.justoneplanet.info/2009/01/18/javascript-object/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
