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

提供: yonewiki
移動: 案内, 検索
(.seal 関数)
(.isSealed 関数)
2,028行: 2,028行:
  
 
== .isSealed 関数 ==
 
== .isSealed 関数 ==
*
+
*Object.isSealed(obj000);
:
+
:上記のようにすると、引数のオブジェクトでプロパティ値の拡張が不可か?データ記述子のconfiguratableがflaseかを評価し、いずれも不可やfalseであれば、trueとなる関数です。サンプルは、評価している内容がわかりやすいようにしたサンプルです。
 +
 
  
 
サンプル
 
サンプル
 
<syntaxhighlight lang="javascript" line start="1">
 
<syntaxhighlight lang="javascript" line start="1">
 +
<HTML>
 +
<HEAD>
 +
<TITLE>JavaScript seal</TITLE>
 +
</HEAD>
 +
<BODY>
 +
JavaScript seal<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("■isSealed<BR />");
 +
document.write("obj000 = ", Object.isSealed(obj000), "<BR />");
 +
document.write("obj001 = ", Object.isSealed(obj001), "<BR />");
 +
document.write("obj010 = ", Object.isSealed(obj010), "<BR />");
 +
document.write("obj011 = ", Object.isSealed(obj011), "<BR />");
 +
document.write("obj100 = ", Object.isSealed(obj100), "<BR />");
 +
document.write("obj101 = ", Object.isSealed(obj101), "<BR />");
 +
document.write("obj110 = ", Object.isSealed(obj110), "<BR />");
 +
document.write("obj111 = ", Object.isSealed(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("■オブジェクト拡張不能化後のisSeal<BR />");
 +
document.write("obj000 = ", Object.isSealed(obj000), "<BR />");
 +
document.write("obj001 = ", Object.isSealed(obj001), "<BR />");
 +
document.write("obj010 = ", Object.isSealed(obj010), "<BR />");
 +
document.write("obj011 = ", Object.isSealed(obj011), "<BR />");
 +
document.write("obj100 = ", Object.isSealed(obj100), "<BR />");
 +
document.write("obj101 = ", Object.isSealed(obj101), "<BR />");
 +
document.write("obj110 = ", Object.isSealed(obj110), "<BR />");
 +
document.write("obj111 = ", Object.isSealed(obj111), "<BR />");
 +
 +
obj111.property1 = 100;
 +
 +
Object.seal(obj111);
 +
document.write("obj111 = ", Object.isSealed(obj111), "<BR />");
 +
 +
obj111.property2 = 1000;
 +
 +
document.write("■obj111.property2の保持値 拡張できない!<BR />");
 +
document.write("obj111.property2.value = ", obj111.property2, "<BR />");
 +
 +
var propertydescriptor3 = Object.getOwnPropertyDescriptor(obj111, "property1");
 +
 +
document.write("■obj111.property1のデータ記述子<BR />");
 +
for(var property in propertydescriptor3){
 +
    document.write("property = ", property, ", propertydescriptor3[", property ,"] = ", propertydescriptor3[property], "<BR />");
 +
}
 +
 +
 +
obj111.property1 = 200;
 +
 +
document.write("■obj111.property1の保持値 変更はできる!<BR />");
 +
document.write("obj111.property1.value = ", obj111.property1, "<BR />");
 +
 +
 +
 +
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 isSealed.html|実行結果サンプル]]
 
[[Media:JavaScript Object isSealed.html|実行結果サンプル]]
 
  
 
== .is 関数 ==
 
== .is 関数 ==

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



個人用ツール
名前空間

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