简体   繁体   English

对象文字可重用函数

[英]Object literal reusable function

Please find the below code for more information. 请查看以下代码以获取更多信息。

var person = {
    firstname:"john",
    lastname:"doe",
    greet: function(){
        return "hello " + this.firstname + " " + this.lastname; 
    }
}

console.log(person.greet());

How can I make above object literal function greet() dymanic? 如何使上面的对象文字函数greet() dymanic? Like I can pass params values for example person.greet('jessie','jay') will return hello jessie jay 就像我可以通过params值例如person.greet('jessie','jay')将返回hello jessie jay

Is it better to use constructor method (instances) in order to make function dynamic and reusable? 使用构造函数方法(实例)以使函数动态化和可重用是否更好? I found that object literal is just to organize code. 我发现object literal只是用来组织代码。

I'll suggest you to use Constructor function. 我建议你使用Constructor函数。 This pattern is called as Factory pattern . 此模式称为工厂模式 The function here acts as class and for each person a new object can be created. 此处的函数充当类,并且可以为每个人创建新对象。

function Person(firstname, lastname) {
    this.firstname = firstname;
    this.lastname = lastname;
}

Person.prototype.greet = function () {
    return 'Hello ' + this.firstname + ' ' + this.lastname;
};

var jon = new Person('Jon', 'Skeet');
var tushar = new Person('Tushar', 'Jadhav');

console.log(jon.greet()); // Hello Jon Skeet
console.log(tushar.greet()); // Hello Tushar Jadhav

First, you should think about what person and greet actually is or should be. 首先,你应该考虑实际上或应该是什么person greet If person is an object and greet a method that operates on the data of that object, the way you've written person is fine. 如果person是一个对象并且greet一个对该对象的数据进行操作的方法,那么你写person方式就没问题了。

If you consider person to be some kind of namespace or module you will use to organize your code, you will write greet as a pure function that doesn't depend on and modify any variables out of its scope. 如果您认为person是某种命名空间或模块,您将用它来组织您的代码,您将把greet写成一个纯函数,它不依赖于并修改其范围之外的任何变量。 In this case you won't have person instance data as firstname and lastname on person . 在这种情况下,你会不会有人员实例数据作为名字和姓氏的person

var person = {
    greet: function(person){
        return "hello " + person.firstName+ " " + person.lastName; 
    }
};  

var jenny = { firstName : 'Jennifer', lastName : 'IdowntKnow' };
person.greet(jenny);

A combination of both will be very confusing in this case 在这种情况下,两者的结合将非常混乱

var person = {
    firstname:"john",
    lastname:"doe",
    greet: function(firstName, lastName){
        /* .. */
    }
};

person.greet('Jennifer', 'IdowntKnow');  
// hmm, what happens here? Person switched to Jennifer now? What about john? weird code...

The question if you should use a constructor function is more about performance or if you need features like prototypal inheritance. 您是否应该使用构造函数的问题更多的是关于性能还是需要原型继承等功能。

greet: function(fname, lname){
    return "hello " +fname + " " + lname; 
}

if you also want to update firstname and lastname 如果您还想更新名字和姓氏

greet: function(fname, lname){
    this.firstname =fname;
    this.latname =lname;
    return "hello " +fname + " " + lname; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM