--- title: JS_DefineObject slug: Mozilla/Projects/SpiderMonkey/JSAPI_reference/JS_DefineObject tags: - JSAPI Reference - SpiderMonkey translation_of: Mozilla/Projects/SpiderMonkey/JSAPI_reference/JS_DefineObject ---
あるオブジェクトのプロパティとなるオブジェクトを生成する関数です。 Create an object that is a property of another object. {{ 英語版章題("Syntax") }}
JSObject * JS_DefineObject(JSContext *cx, JSObject *obj,
const char *name, JSClass *clasp, JSObject *proto,
uintN flags);
| Name | Type | Description |
|---|---|---|
cx |
JSContext * |
エラー報告に用いるランタイム環境から派生したコンテキストへのポインタPointer to a JS context from which to derive runtime information for error reporting. {{ Jsapi-requires-request() }} |
obj |
JSObject * |
生成したオブジェクトをプロパティとして持つオブジェクトへのポインタObject to which this new object belongs as a property. |
name |
const char * |
objでのプロパティ名Name of the property that encapsulates the new object in <code>obj</code>. |
clasp |
JSClass * |
オブジェクト生成に用いるクラスClass to use for the new object. |
proto |
JSObject * |
オブジェクト生成に用いるプロトタイプPrototype object to use for the new object. |
flags |
uintN |
生成したオブジェクトの属性を指定するフラグProperty flags for the new object. |
{{ 英語版章題("Description") }}
JS_DefineObjectは、オブジェクトのインスタンスを生成し、それを引数objで指定した既存オブジェクトのプロパティとして設定する関数です。引数nameで、objでのプロパティ名を指定し、flagsでそのプロパティの属性を指定します。以下の表で設定可能なflagsの値を示します。これらの値は単一、あるいは複数の値の論理和として引数に与えることができます。 <code>JS_DefineObject</code> instantiates and names a new object for an existing object, <code>obj</code>. <code>name</code> is the property name to assign to <code>obj</code> to hold the new object, and <code>flags</code> contains the property flags to set for the newly created property. The following table lists possible values you can pass in <code>flags</code>, either singly, or <code>OR</code>'d together:
| Flag | Purpose |
|---|---|
JSPROP_ENUMERATE |
forとinループを使ったアクセス(Core JavaScript 1.5 Guide:Object Manipulation Statements)を可能にします。Property is visible to <code>for</code> and <code>in</code> loops. |
JSPROP_READONLY |
プロパティの値を読み取り専用にします。Property is read only. |
JSPROP_PERMANENT |
プロパティの削除を不可能にします。Property cannot be deleted. |
JSPROP_EXPORTED |
Property can be imported by other objects. |
JSPROP_INDEX |
Property is actually an index into an array of properties, and is cast to a const char *. |
引数claspはオブジェクトの生成時に用いる基底クラスへのポインタ、引数protoはプロトタイプへのポインタをそれぞれとります。protoにNULLを指定した場合、JavaScriptは自分自身をプロトタイプとします。引数objはオブジェクトの親オブジェクトになります。 <code>clasp</code> is a pointer to the base class to use when creating the new object, and <code>proto</code> is an pointer to the prototype upon which to base the new object. If you set <code>proto</code> to <code>NULL</code>, JS sets the prototype object for you. The parent object for the new object is set to <code>obj</code>.
JS_DefineObjectが成功したとき、返値は新たに生成したプロパティオブジェクトへのポインタになります。もしすでにプロパティが設定されていた場合、オブジェクトは生成されず、返り値はNULLになります。 <code>JS_DefineObject</code> returns a pointer to the newly created property object if successful. If the property already exists, or cannot be created, <code>JS_DefineObject</code> returns <code>NULL</code>. {{ 英語版章題("See Also") }}
| Groups | Functions |
| Documents | {{ LXRSearch("ident", "i", "JS_DefineObject", "LXR ID Search") }} |
| Entries |
JSClass, JS_DefineConstDoubles, JS_DefineElement, JS_DefineFunction, JS_DefineFunctions, JS_DefineProperties, JS_DefineProperty, JS_DefinePropertyWithTinyId, JS_NewObject, JS_ValueToObject |
{{ languages( { "en": "en/JS_DefineObject" } ) }}