0

MooToolsを使って新しいクラスを作成しました。私のクラスはこんな感じ

更新しました:

var c=new Class({
    a:'',
    b:'',
    c:'',
    d:'',
initialize:function(ee){
this.e=ee;
},
buildJSON:function()
{
var cInstance=new c(this.e);
cInstance.a=this.a;
cInstance.b=this.b;
cInstance.c=this.c;
cInstance.d=this.d;

return (JSON.encode(cInstance));
}
});

var x=new c("action");
x.a="Hello a";
x.b="Hello b";
x.c="Hello c";
x.d="Hello d";

alert (x.buildJSON());​

これはかなり単純なクラスです。これを試してみると、JSONに追加のキーがあります。

"$caller":null,
"caller":null
4

1 に答える 1

4

$caller and caller are both properties added by MooTools Class.

They exist to assist in the use of the parent method. You should clone the object and clean out the unnecessary properties before using JSON.encode on the class instance.

You could clone this and delete $caller and caller from the clone.

var c=new Class({
    a:'',
    b:'',
    c:'',
    d:'',

    initialize: function(ee) {
        this.e=ee;
    },

    buildJSON: function() {
        var data = Object.clone(this);
        delete data.$caller;
        delete data.caller;

        return (JSON.encode(data));
    }
});

var x=new c("action");
x.a="Hello a";
x.b="Hello b";
x.c="Hello c";
x.d="Hello d";

alert (x.buildJSON());​
于 2012-07-27T23:45:15.403 に答える