码迷,mamicode.com
首页 > 其他好文 > 详细

constructor

时间:2018-07-27 18:06:33      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:new   amp   char   function   title   pre   range   html   body   

防篡改: 禁止修改对象的属性结构
 防扩展: 禁止向对象中添加新属性
Object.preventExtensions(obj)
 密封: 即防扩展,又禁止删除旧属性
Object.seal(obj)
其实是将所有属性的configurable设置为false

 

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
"use strict";

function Emp(id,name,salary,age) {
//this->新对象
//id只读,禁止删除
//name禁止删除
//salary禁止删除,禁止遍历
//age:18-65之间
this.id=id;
this.name=name;
this.salary=salary;

var _age;
Object.defineProperties(this,{
id:{writable:false},
salary:{enumerable:false},
age:{
get(){return _age;},
set(val) {
if (val>=18&&val<=65)
_age=val;
else
throw new RangeError("年龄必须介于18-65之间");
},
enumerable:true
}
});
this.age=age;

//防扩展
//设置当前新对象禁止扩展新属性
// Object.preventExtensions(this);
//密封
Object.seal(this);
}
var eric=new Emp(1001,"Eric",10000,25);
// eric.is++;
// delete eric.id;
// eric.age=-2;
console.dir(eric);
</script>
</body>
</html>

constructor

标签:new   amp   char   function   title   pre   range   html   body   

原文地址:https://www.cnblogs.com/sske531549304/p/9378914.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!