如何将方法添加到(JSON)对象的原型? [英] How to add methods to a (JSON) object's prototype?

查看:114
本文介绍了如何将方法添加到(JSON)对象的原型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我从服务器收到一些JSON对象,例如Person对象的一些数据:

Let's say I receive some JSON object from my server, e.g. some data for a Person object:

{firstName: "Bjarne", lastName: "Fisk"}

现在,我想要在这些数据之上的一些方法,例如用于计算fullName:

Now, I want some methods on top of those data, e.g. for calculating the fullName:

fullName: function() { return this.firstName + " " + this.lastName; }

这样我就可以

var personData = {firstName: "Bjarne", lastName: "Fisk"};
var person = PROFIT(personData);
person.fullName(); // => "Bjarne Fisk"

我基本上想在这里做的是为对象添加一个方法原型。 fullName()方法是通用的,因此不应添加到数据对象本身。喜欢..:

What I basically would want to do here, is to add a method to the object's prototype. The fullName() method is general, so should not be added to the data object itself. Like..:

personData.fullName = function() { return this.firstName + " " + this.lastName; }

...会导致大量冗余;并且可以说是污染数据对象。

... would cause a lot of redundancy; and arguably "pollute" the data object.

将这些方法添加到简单数据对象的当前最佳实践方法是什么?

What is the current best-practice way of adding such methods to a simple data object?

编辑:

稍微偏离主题,但如果上述问题可以解决,则可以做到一些不错的伪 - 模式匹配,如下所示:

Slightly off topic, but if the problem above can be solved, it would be possible to do some nice pseudo-pattern matching like this:

if ( p = Person(data) ) {
   console.log(p.fullName());
} else if ( d = Dog(data) ) {
   console.log("I'm a dog lol. Hear me bark: "+d.bark());
} else {
   throw new Exception("Shitty object");
}

和<如果 data 对象具有正确的属性,code> Dog 将添加方法。如果没有,返回falsy(即数据匹配/符合)。

Person and Dog will add the methods if the data object has the right attributes. If not, return falsy (ie. data does not match/conform).

奖励问题:是否有人知道一个使用或启用它的库(即使它变得容易)?它已经是一个JavaScript模式?如果是这样,它叫什么;你有一个详细阐述的链接吗?谢谢:)

BONUS QUESTION: Does anyone know of a library that either uses or enables this (ie makes it easy)? Is it already a javascript pattern? If so, what is it called; and do you have a link that elaborates? Thanks :)

推荐答案

假设您的Object来自一些解析服务器输出以生成Object的JSON库,它将不在一般在其原型中有任何特别之处;并且为不同的服务器响应生成的两个对象将不共享原型链(当然除了Object.prototype;)

Assuming your Object comes from some JSON library that parses the server output to generate an Object, it will not in general have anything particular in its prototype ; and two objects generated for different server responses will not share a prototype chain (besides Object.prototype, of course ;) )

如果你控制所有人员的地方是从JSON创建的,你可以反过来做事:创建一个空Person对象(在其原型中使用类似fullName的方法),并使用从JSON生成的对象扩展它(使用$ .extend,_ .extend,或类似的东西)。

If you control all the places where a "Person" is created from JSON, you could do things the other way round : create an "empty" Person object (with a method like fullName in its prototype), and extend it with the object generated from the JSON (using $.extend, _.extend, or something similar).

var p = { first : "John", last : "Doe"};

function Person(data) {
   _.extend(this, data);
}

Person.prototype.fullName = function() {
   return this.first + " " + this.last;   
}

console.debug(new Person(p).fullName());

这篇关于如何将方法添加到(JSON)对象的原型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆