JavaScript オブジェクト 新しいページはコチラ

提供: yonewiki
移動: 案内, 検索
(.setPrototypeOf 関数)
(.freeze 関数)
1,413行: 1,413行:
  
 
== .freeze 関数 ==
 
== .freeze 関数 ==
*
+
*Object.freeze(obj);
:
+
:と上記のようにすると引数に指定したobjオブジェクトはプロパティ記述子の状態が変化し、writableとconfigurableとがfalseに変化し、objのプロパティ値の変更や削除ができなくなります。
  
 
サンプル
 
サンプル
 
<syntaxhighlight lang="javascript" line start="1">
 
<syntaxhighlight lang="javascript" line start="1">
 +
<HTML>
 +
<HEAD>
 +
<TITLE>JavaScript freeze</TITLE>
 +
</HEAD>
 +
<BODY>
 +
JavaScript freeze<br />
 +
<SCRIPT Language="JavaScript">
 +
<!--
 +
var obj = {
 +
    x:{
 +
        value:"data1",
 +
        writable:true,
 +
        enumerable:true,
 +
        configurable:true
 +
    }
 +
};
  
 +
function objFunc(){
 +
    this.nValue1 = 100;
 +
    this.nValue2 = 200;
 +
}
 +
 +
Object.freeze(obj);
 +
 +
//オブジェクトの拡張 フリーズしているため無効
 +
obj.property1 = 100;
 +
 +
var propertydescriptor = Object.getOwnPropertyDescriptor(obj, "x");
 +
 +
for(var property in propertydescriptor){
 +
    document.write("property = ", property, ", propertydescriptor[", property ,"] = ", propertydescriptor[property], "<BR />");
 +
}
 +
document.write("obj.x.value = ", obj.x.value, "<BR />");
 +
 +
obj.x = {
 +
        value:"data2",
 +
        writable:true,
 +
        enumerable:true,
 +
        configurable:true
 +
    };
 +
 +
document.write("obj.x.value = ", obj.x.value, "<BR />");
 +
 +
delete obj.x;
 +
 +
document.write("obj.x.value = ", obj.x.value, "<BR />");
 +
 +
 +
-->
 +
</SCRIPT>
 +
</BODY>
 +
</HTML>
 
</syntaxhighlight>
 
</syntaxhighlight>
 
[[Media:JavaScript Object freeze.html|実行結果サンプル]]
 
[[Media:JavaScript Object freeze.html|実行結果サンプル]]
 
  
 
== .isFrozen 関数 ==
 
== .isFrozen 関数 ==

2016年2月29日 (月) 00:00時点における版



個人用ツール
名前空間

変種
操作
案内
ツールボックス