【发布时间】:2011-05-05 01:32:24
【问题描述】:
我正在从服务器接收一些 JSON 对象,我想将其“类型转换”或“祝福”到具有已定义方法的对象。有没有办法为纯 JSON 对象设置原型?
function MyClass(someValue) {
this.myProperty = someValue;
}
MyClass.prototype.someMethod = function() { return "Here's " + this.myProperty + "!"};
var json = {myProperty : 'someValue'};
// ??? json.prototype = MyClass doesn't work, of course.
var result = json.someMethod();
我该怎么做?
【问题讨论】:
标签: javascript oop json casting