问题描述
我在这里读了很多关于JS访问并找出这会是对我好:
这是我用于本地字段:
TYPE_DEFAULT_VALUE = {
数字:0,
字符串:,
数组:[],
对象:{},
};的typeof =功能(对象){
如果(typeof运算对象===号与&&安培; isNaN(对象))
返回结果为NaN;
尝试{
返回Object.prototype.toString.call(对象).slice(8,-1).toLowerCase();
}
赶上(前){
返回N / A;
};
};getAccessor =功能(OBJ,钥匙,类型,设置defaultValue){
如果(设置defaultValue ===未定义)
设置defaultValue = TYPE_DEFAULT_VALUE【类型】===未定义?空:TYPE_DEFAULT_VALUE【类型】;
返回{
枚举:真实,
配置:真实,
得到:函数(){
如果(OBJ [关键] ===未定义)
OBJ [关键] =设置defaultValue;
返回OBJ [关键]
},
设置:功能(值){
如果(typeof运算(值)===型)
OBJ [关键] =价值;
},
};
}LocalFields =功能(领域对象){
/ **
*字段属性
* {
*类型:[必需](数字|字符串|数组|对象| ...)
*设置defaultValue:[可选]
*}
* /
如果(!字段)
扔太少参数......;
如果(!对象)
目标=这一点; VAR OBJ =这一点;
变种fieldsAccessor = {};
对(在领域的关键){
字段=字段[关键]
fieldHandler =键[0] .toUpperCase()+ key.substr(1);
如果(!field.type)
抛类型不设现场:+键; fieldsAccessor [fieldHandler] = getAccessor(OBJ,fieldHandler,field.type,field.defaultValue)
}
Object.defineProperties(对象,fieldsAccessor);
}
现在为每个类我可以只调用是这样的:
=人功能(){
新LocalFields({
ID:{类型:数字},
名称:{类型:串},
电话:{类型:阵},
}, 这个);
}
然后像VS getter和setter你会打电话:
VAR亚历克斯=新的Person();
alex.Name =亚历克斯援助团;
console.clear();
console.info(alex.Name);
这适用于所有类型的,但有一个问题,因为getter和setter是基本的操作,如果我想补充一个数组字段,调用此append方法,甚至prePEND还有什么无论如何反正做到这一点?
例如,我怎么能说:
alex.Phone.append('+ 1234567890');
这是一个很好的努力,但你忘了,有数组列表中没有任何附加的功能!
您可以使用推和任何其他阵列功能。检查一遍;
I read a lot about JS accessors here and figure out this gonna be good for me:
This is what I used for local fields:
TYPE_DEFAULT_VALUE= {
number: 0,
string: "",
array: [],
object: {},
};
typeOf = function (object) {
if (typeof object === "number" && isNaN(object))
return NaN;
try {
return Object.prototype.toString.call(object).slice(8, -1).toLowerCase();
}
catch(ex) {
return "N/A";
};
};
getAccessor = function(obj, key, type, defaultValue) {
if (defaultValue === undefined)
defaultValue = TYPE_DEFAULT_VALUE[type] === undefined ? null : TYPE_DEFAULT_VALUE[type];
return {
enumerable: true,
configurable: true,
get: function () {
if (obj[key] === undefined)
obj[key] = defaultValue;
return obj[key];
},
set: function (value) {
if (typeOf(value) === type)
obj[key] = value;
},
};
}
LocalFields = function (fields, object) {
/**
* field properties
* {
* type: [ required ] ( number | string | array | object | ... ),
* defaultValue: [ optional ]
* }
*/
if (! fields)
throw "Too few parameters ...";
if (! object)
object = this;
var obj = this;
var fieldsAccessor = {};
for(key in fields){
field = fields[key];
fieldHandler = key[0].toUpperCase() + key.substr(1);
if(! field.type)
throw "Type not set for field: " + key;
fieldsAccessor[fieldHandler] = getAccessor(obj, fieldHandler, field.type, field.defaultValue)
}
Object.defineProperties(object, fieldsAccessor);
}
Now for each Class I can just call something like:
Person = function(){
new LocalFields({
id: { type: "number" },
name: { type: "string" },
phone: { type: "array" },
}, this);
}
And then like VS getter and setter you'll call:
var alex = new Person();
alex.Name = "Alex Ramsi";
console.clear();
console.info(alex.Name);
this works for all types but there is a problem because getter and setter is the basic operation and what if I want to add an array field and call this append method or even prepend is there anyhow anyway to do that?For example How can I call:
alex.Phone.append('+1234567890');
That's a good effort but you forgot that there is no append function for array list!You can use push and any other array functionality. Check it again;
这篇关于JavaScript的超越属性访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!