Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript/AngularJS:将函数绑定到每个对象还是生成帮助对象?_Javascript_Angularjs_Function_Oop - Fatal编程技术网

Javascript/AngularJS:将函数绑定到每个对象还是生成帮助对象?

Javascript/AngularJS:将函数绑定到每个对象还是生成帮助对象?,javascript,angularjs,function,oop,Javascript,Angularjs,Function,Oop,只是一个快速的想法,我不知道如何谷歌它 如果您从带有AngularJS的JSON格式API中获得一个let列表,比如“persons”,并将其转换为JS数组/对象: [ {firstName: 'Foo', lastName: 'Bar'}, {firstName: 'Banana', lastName: 'Fruit'} ] 您需要一些将名字和姓氏连接到全名的通用函数 那么,一般认为使用解决方案1或2更好吗 解决方案1 数组由对象组成,这些对象在自身内部具有所需的所有方法 [ {

只是一个快速的想法,我不知道如何谷歌它

如果您从带有AngularJS的JSON格式API中获得一个let列表,比如“persons”,并将其转换为JS数组/对象:

[
  {firstName: 'Foo', lastName: 'Bar'},
  {firstName: 'Banana', lastName: 'Fruit'}
]
您需要一些将名字和姓氏连接到全名的通用函数

那么,一般认为使用解决方案1或2更好吗

解决方案1 数组由对象组成,这些对象在自身内部具有所需的所有方法

[
  {firstName: 'Foo', lastName: 'Bar', fullName: function(){return this.firstName+' '+this.lastName;}},
  {firstName: 'Banana', lastName: 'Fruit', fullName: function(){return this.firstName+' '+this.lastName;}}
]
我认为这是最为面向对象的解决方案,但对于Javascript(和长列表)来说也是最好的解决方案吗

解决方案2 生成只实例化一次的助手函数

var helper = function(person)
{
   return person.firstName+' '+person.lastName;
}

我知道这是非常基本的,但有时这是最好的选择;)

在您的第一个解决方案中,您将向每个对象添加一个函数,这感觉很重,如果您有很多对象,并且如果您迭代其他对象,我认为这不是最好的

第二个更轻

另一个想法是:

也许您可以将JSON解析为特定对象,如下所示:

然后使用原型

Foo.prototype.fullname = function (){
    return this.firstName+' '+this.lastName;
}

因此,每个Foo对象都可以通过原型调用fullname()(在内存中只调用一次vs在每个对象中调用一次)。

在您的第一个解决方案中,您将向每个对象添加一个函数,这感觉很重,如果您有很多对象,如果您迭代其他对象,我认为这不是最好的

第二个更轻

另一个想法是:

也许您可以将JSON解析为特定对象,如下所示:

然后使用原型

Foo.prototype.fullname = function (){
    return this.firstName+' '+this.lastName;
}

因此,每个Foo对象都可以通过原型调用fullname()(在内存中,每个对象只调用一次vs)。

Boris Charpentier已经给出了一个很好的答案。我可以添加一些示例代码:

可以通过原始JSON对象初始化自身的构造函数:

function Workout(workoutJson) {
    if (workoutJson) {
        this.duration = workoutJson.duration;
        ... // or copy all properties in a for-loop
    }
}

var fn = Workout.prototype;

// extend the prototype with some functions
fn.getSomeData = function () {
    return this.onePart + ' ' + this.anotherPart;
};

// another smart solution: get properties (modern browser necessary)
Object.defineProperties(fn, {
    durationMinutes: {
        enumerable: true,
        get: function () {
            return roundMillisToMinutes(this.duration);
        },
        set: function (val) {
            this.duration = val * 60000;
        }
    }
});
现在您有了一个属性
durationMinutes
,它的行为类似于一个实属性,但包含一个getter和setter函数

让我们添加一些静态函数(无需实例),稍后我们将使用它们将原始JSON对象转换为域对象:

Workout.fromJson = function(workoutJson) {
    return new Workout(workoutJson);
};

Workout.fromJsonArray = function(workoutJsonArray) {
    return workoutJsonArray.map(Workout.fromJson);
};
在我的数据访问服务中,我正在将原始数据转换为我的域模型的实例(在本例中):

备注:我知道有更好的方法来处理$q承诺,并做一些链接,而不是使用$q。这是一些早期的工作与角度。这里最重要的一点是如何将原始JSON数据转换为JavaScript“构造”对象(在其
原型中使用辅助方法或属性)

编辑


我在博客中找到了一篇关于这方面的有趣文章。

您已经有了Boris Charpentier的一个很好的答案。我可以添加一些示例代码:

可以通过原始JSON对象初始化自身的构造函数:

function Workout(workoutJson) {
    if (workoutJson) {
        this.duration = workoutJson.duration;
        ... // or copy all properties in a for-loop
    }
}

var fn = Workout.prototype;

// extend the prototype with some functions
fn.getSomeData = function () {
    return this.onePart + ' ' + this.anotherPart;
};

// another smart solution: get properties (modern browser necessary)
Object.defineProperties(fn, {
    durationMinutes: {
        enumerable: true,
        get: function () {
            return roundMillisToMinutes(this.duration);
        },
        set: function (val) {
            this.duration = val * 60000;
        }
    }
});
现在您有了一个属性
durationMinutes
,它的行为类似于一个实属性,但包含一个getter和setter函数

让我们添加一些静态函数(无需实例),稍后我们将使用它们将原始JSON对象转换为域对象:

Workout.fromJson = function(workoutJson) {
    return new Workout(workoutJson);
};

Workout.fromJsonArray = function(workoutJsonArray) {
    return workoutJsonArray.map(Workout.fromJson);
};
在我的数据访问服务中,我正在将原始数据转换为我的域模型的实例(在本例中):

备注:我知道有更好的方法来处理$q承诺,并进行一些链接,而不是使用$q。这是Angular的一些早期工作。这里的重点是在何处以及如何将原始JSON数据转换为JavaScript“构造”对象(在其
原型中包含辅助方法或属性)

编辑


我在博客中找到了一篇关于这一点的有趣文章。

所以原型是静态的,因此是解决方案1的一个很好的实现?事实上,你是对的!从来都不知道原型的这个重要用例--昨天我自己就发现了;)所以prototype是静态的,因此是解决方案1的一个很好的实现?事实上,你是对的!从来都不知道prototype的这个重要用例--昨天我自己就发现了;)稍后将对此进行更仔细的研究。谢谢调查!稍后我们将对此进行更仔细的研究。谢谢调查!