Advertisement
Guest User

Untitled

a guest
Sep 30th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. // Object.seal()メソッド
  2. // 「オブジェクトの封印」を行う
  3. // 封印されたオブジェクトは、プロパティの「追加」「削除」「属性変更」ができなくなる
  4. // プロパティの読み書きのみが許可された状態になる
  5. //
  6. // Object.isSealed()メソッド
  7. // オブジェクトの封印状況を確認する
  8.  
  9. let human = {
  10. name: 'igarashi'
  11. };
  12.  
  13. console.log(Object.isSealed(human)); // false
  14.  
  15. // プロパティを追加してみる
  16.  
  17. human.sex = 'M';
  18. human.age = 35;
  19.  
  20. console.log('sex' is human); // true
  21. console.log('age' is human); // true
  22.  
  23. // プロパティを削除してみる
  24. delete human.sex;
  25.  
  26. console.log('sex' in human); // false;
  27.  
  28. // オブジェクトを封印する
  29. Object.seal(human);
  30.  
  31. console.log(Object.isSealed(human)); // true
  32.  
  33. // プロパティの削除を試みる
  34. delete human.age; // Strictモードならここでエラー
  35.  
  36. console.log('age' in human); // true
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement