JavaScript OOP动态getter setter-我做错了什么
我一整天都在努力想办法让这一切顺利进行。我真的很烦。我已经阅读了大量关于OOP的文章,虽然我知道我的代码并不完美,但我似乎无法回避我在下面的代码中概述的最初问题 除了动态的JavaScript OOP动态getter setter-我做错了什么,javascript,oop,Javascript,Oop,我一整天都在努力想办法让这一切顺利进行。我真的很烦。我已经阅读了大量关于OOP的文章,虽然我知道我的代码并不完美,但我似乎无法回避我在下面的代码中概述的最初问题 除了动态的getter/setter,所有的代码都可以工作-如果您阅读我在代码中的注释或运行代码,您可以很容易地看到原因 我有一种感觉,我想做的事做不到。有人能告诉我哪里出了问题,或者如果不能按照我的建议去做 var ext = { db: { string: functi
getter/setter
,所有的代码都可以工作-如果您阅读我在代码中的注释或运行代码,您可以很容易地看到原因
我有一种感觉,我想做的事做不到。有人能告诉我哪里出了问题,或者如果不能按照我的建议去做
var ext = {
db: {
string: function(def){
def=def || {};
def.type = "string";
return def;
},
autoId: function(def){
def=def || {};
def.defaultvalue = function(){
return Math.uuid(def.length, def.radix)
return def;
}
}
},
}
/*
* @ext.base: base class I can build out with common methods
*
*/
ext.base = function(){
/*
* place holder for the raw object data -
*
* {a:"martin webb",b:"is struggling with this"
*
*/
this._rawdata={}; // this can later be strigified for storage;
};
ext.base.prototype ={
init:function(o){
// pass in rawdata;
this.load(o);
},
put:function(){
//code removed
},
load: function(rawdata){
this._rawdata=rawdata || {};
}
}
/*
* @ext.kind: builds a new object we can use to make entities from our kind
* martin=new people();
*
*/
ext.db.kind=function(o){
this._kind=function(o){this.init(o);} //this will pass any passed in object in the base class at init;
this._kind.prototype = ext.base.prototype;
var self=this;
var prop,i=97; //ascii for "a";
for (prop in o){
if (o.hasOwnProperty(prop)) {
/*
* This is like a dynamic getter setter, we are adding it to the prototype;
*
* if you run the code when the function below is called
* value="martin webb" I need the var i to equal 97, and prop to = "name"
* in the function. I need a way to pass in and conserver these dynamic values
*/
this._kind.prototype[prop] =
function(value){
if (value) {
self.rawdata[i] = value; //ERROR i=99!
}
return self.rawdata[i] ; //get the correct data using the key
};
i++;
}
}
return this._kind; // return our new object so we can use it like var martin=new people();
}
debugger;
//lets make a new kind;
var people={};
people.name=ext.db.string();
people.info=ext.db.string();
people = new ext.db.kind(people);
//and now make some entities usinhg it;
var martin=new people();
//THE CODE WILL GO WRONG HERE, THE SETTER WILL NOT WORK (SEE ABOVE)
martin.name("martin webb")
martin.info("is struggling with this code")
var mike = new people({
a: "mike",
b: "has the solution"
});
console.log(mike.name()) //--> mike
我找到了解决办法。答案是使用一个外壳并捕获循环索引(i) 作者在文章中对此做了漂亮的解释 对我的代码的修改如下,效果不错
var prop, i = 97; //ascii for "a";
//loop each property and make our own get/setter that works
//off our underlying compressed rawdata;
for (prop in o) {
if (o.hasOwnProperty(prop)) {
/*
* this is the trick
* use an enclosure and pass our loop index in the bottom
* with a self calling function, this locks the value of i;
*/
this._kind.prototype[prop] = function(k){
return function(value){
var key=String.fromCharCode(k);
//if we have a value passed in set it!
if (value){
this._rawdata[key]=value;
}
//always return our value;
return this._rawdata[key];
};
}(i); //here we call our function and lock index i;
i++;
}//end loop;