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

提供: yonewiki
移動: 案内, 検索
(.preventExtensions 関数)
(.isExtensible 関数)
1,664行: 1,664行:
  
 
== .isExtensible 関数 ==
 
== .isExtensible 関数 ==
*
+
*Object.isExtensible(obj000)
:
+
:上記のように引数として付与したオブジェクトにぶら下がっているプロパティが拡張不能になっているかを評価し、拡張可能ならばtrue、拡張不可能ならばfalseを返却する関数です。isFrozenが、拡張不能+writeble:false+configuratable:falseでtrueという評価であるので、できないならtrueという形式をとっているのに対して、こちらは拡張できるならtrueという相反するような評価手法になっていることに注意が必要です。勘違いしないようにしましょう。紛らわしいですが、英語ができる人なら関数名からピンとくるんです。日本人にはつらいよね。先述のサンプルと似ていますが、isExtensibleという評価に変更したサンプルです。
  
 
サンプル
 
サンプル
 
<syntaxhighlight lang="javascript" line start="1">
 
<syntaxhighlight lang="javascript" line start="1">
 +
<HTML>
 +
<HEAD>
 +
<TITLE>JavaScript isExtensible</TITLE>
 +
</HEAD>
 +
<BODY>
 +
JavaScript isExtensible<br />
 +
<SCRIPT Language="JavaScript">
 +
<!--
 +
var obj000 = {
 +
    x000:{
 +
        value:"data000",
 +
    }
 +
};
 +
var obj001 = {
 +
    x001:{
 +
        value:"data001",
 +
    }
 +
};
 +
var obj010 = {
 +
    x010:{
 +
        value:"data010",
 +
    }
 +
};
 +
var obj011 = {
 +
    x011:{
 +
        value:"data011",
 +
    }
 +
};
 +
var obj100 = {
 +
    x100:{
 +
        value:"data100",
 +
    }
 +
};
 +
var obj101 = {
 +
    x101:{
 +
        value:"data101",
 +
    }
 +
};
 +
var obj110 = {
 +
    x110:{
 +
        value:"data110",
 +
    }
 +
};
 +
var obj111 = {
 +
    x111:{
 +
        value:"data111",
 +
    }
 +
};
 +
Object.defineProperty(obj000, "x000", {
 +
    value: "data000",
 +
    writable: false,
 +
    enumerable: false,
 +
    configurable: false
 +
});
 +
Object.defineProperty(obj001, "x001", {
 +
    value: "data001",
 +
    writable: false,
 +
    enumerable: false,
 +
    configurable: true
 +
});
 +
Object.defineProperty(obj010, "x010", {
 +
    value: "data010",
 +
    writable: false,
 +
    enumerable: true,
 +
    configurable: false
 +
});
 +
Object.defineProperty(obj011, "x011", {
 +
    value: "data011",
 +
    writable: false,
 +
    enumerable: true,
 +
    configurable: true
 +
});
 +
Object.defineProperty(obj100, "x100", {
 +
    value: "data100",
 +
    writable: true,
 +
    enumerable: false,
 +
    configurable: false
 +
});
 +
Object.defineProperty(obj101, "x101", {
 +
    value: "data101",
 +
    writable: true,
 +
    enumerable: false,
 +
    configurable: true
 +
});
 +
Object.defineProperty(obj110, "x110", {
 +
    value: "data110",
 +
    writable: true,
 +
    enumerable: true,
 +
    configurable: false
 +
});
 +
Object.defineProperty(obj111, "x111", {
 +
    value: "data111",
 +
    writable: true,
 +
    enumerable: true,
 +
    configurable: true
 +
});
  
 +
function objFunc(){
 +
    this.nValue1 = 100;
 +
    this.nValue2 = 200;
 +
}
 +
document.write("■isExtensible<BR />");
 +
document.write("obj000 = ", Object.isExtensible(obj000), "<BR />");
 +
document.write("obj001 = ", Object.isExtensible(obj001), "<BR />");
 +
document.write("obj010 = ", Object.isExtensible(obj010), "<BR />");
 +
document.write("obj011 = ", Object.isExtensible(obj011), "<BR />");
 +
document.write("obj100 = ", Object.isExtensible(obj100), "<BR />");
 +
document.write("obj101 = ", Object.isExtensible(obj101), "<BR />");
 +
document.write("obj110 = ", Object.isExtensible(obj110), "<BR />");
 +
document.write("obj111 = ", Object.isExtensible(obj111), "<BR />");
 +
 +
Object.preventExtensions(obj000);
 +
Object.preventExtensions(obj001);
 +
Object.preventExtensions(obj010);
 +
Object.preventExtensions(obj011);
 +
Object.preventExtensions(obj100);
 +
Object.preventExtensions(obj101);
 +
Object.preventExtensions(obj110);
 +
Object.preventExtensions(obj111);
 +
 +
 +
document.write("■オブジェクト拡張不能化後のisExtensible<BR />");
 +
document.write("obj000 = ", Object.isExtensible(obj000), "<BR />");
 +
document.write("obj001 = ", Object.isExtensible(obj001), "<BR />");
 +
document.write("obj010 = ", Object.isExtensible(obj010), "<BR />");
 +
document.write("obj011 = ", Object.isExtensible(obj011), "<BR />");
 +
document.write("obj100 = ", Object.isExtensible(obj100), "<BR />");
 +
document.write("obj101 = ", Object.isExtensible(obj101), "<BR />");
 +
document.write("obj110 = ", Object.isExtensible(obj110), "<BR />");
 +
document.write("obj111 = ", Object.isExtensible(obj111), "<BR />");
 +
 +
Object.freeze(obj111);
 +
 +
//オブジェクトの拡張 フリーズしているため無効
 +
obj111.property1 = 100;
 +
 +
var propertydescriptor = Object.getOwnPropertyDescriptor(obj111, "x111");
 +
 +
document.write("■obj111のデータ記述子<BR />");
 +
for(var property in propertydescriptor){
 +
    document.write("property = ", property, ", propertydescriptor[", property ,"] = ", propertydescriptor[property], "<BR />");
 +
}
 +
document.write("■obj000の保持値<BR />");
 +
document.write("obj000.value = ", obj000.x000, "<BR />");
 +
 +
 +
 +
delete obj000.x000;
 +
 +
document.write("■deleteキーワード使用後、obj000の保持値<BR />");
 +
document.write("obj000.value = ", obj000.x000, "<BR />");
 +
 +
var propertydescriptor000 = Object.getOwnPropertyDescriptor(obj000, "x000");
 +
 +
document.write("■deleteキーワード使用後、obj000のデータ記述子<BR />");
 +
for(var property in propertydescriptor000){
 +
    document.write("property000 = ", property, ", propertydescriptor000[", property ,"] = ", propertydescriptor000[property], "<BR />");
 +
}
 +
 +
-->
 +
</SCRIPT>
 +
</BODY>
 +
</HTML>
 
</syntaxhighlight>
 
</syntaxhighlight>
 
[[Media:JavaScript Object isExtensible.html|実行結果サンプル]]
 
[[Media:JavaScript Object isExtensible.html|実行結果サンプル]]

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



個人用ツール
名前空間

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