1
\$\begingroup\$

After coming across Douglas Crockford's views on class-free OOP, and doing away with new and this in Javascript I tried defining an object with its own variable and function, and returning it in JSON, as shown below:

var newObj = function (name, lang){
    var obj = {
            "name":name,
            "greeter":greeter
    };
    return obj;
};

function greeter (lang){
        switch (lang){
                case "en":return "hi";
                case "es":return "hola";
                default: return "hello";
        }
}


//and finally, use the object    
var name = "john", lang = "es";  
var obj = newObj(name, lang);

console.log(obj.name);
console.log(obj.greeter(lang));

I know this code works in NodeJS, but I wonder if it is actually a good idea to do it this way. More specifically, are there any implications/consequences to returning functions within a javascript object that I should be aware of?

\$\endgroup\$
10
  • 1
    \$\begingroup\$ JSON cannot encode functions. I don't see you using JSON anywhere in that snippet. \$\endgroup\$ Apr 14 '16 at 8:48
  • \$\begingroup\$ var obj = { "name":name, "greeter":greeter }; isn't that a JSON object? \$\endgroup\$
    – Yogesch
    Apr 14 '16 at 9:21
  • \$\begingroup\$ JSON is a data format based on the javascript object initalizer syntax. Your code uses the object initalizer syntax json is based on, but it doesn't use json. \$\endgroup\$ Apr 14 '16 at 9:25
  • \$\begingroup\$ thanks for the clarification. So would it be more appropriate to phrase the question as Returning functions within javascript objects? \$\endgroup\$
    – Yogesch
    Apr 14 '16 at 9:36
  • \$\begingroup\$ Is this real code from a project? It looks more like example code. \$\endgroup\$
    – Jamal
    Apr 14 '16 at 10:06
1
\$\begingroup\$

This is not JSON. However JSON was inspired from Javascript Native way of creating object. This is why they're really similar.

However JSON handles only data, not functions.

Furthermore you can rewrite your function like this :

function newObj(name){
    this.name = name;
    return this;
};
newObj.prototype.greeter = function(lang){
    switch (lang){
            case "en":return "hi "+this.name;
            case "es":return "hola "+this.name;
            default: return "hello "+this.name;
    }
}

var obj = new newObj("name");
obj.greeter("de");

As you can see

  1. I got rid of the 2nd parameter in the newObj function since it was not used.
  2. I bind greeter to the Object so now I can use this.name in greeter to greet you by your name.
  3. If you try to JSONify this, everything under prototype will be ignored so you won't have any error.
\$\endgroup\$
0
0
\$\begingroup\$

Try this ;)

var newObj = function(name, lang) {
  var obj = {
    name: name,
    greeter: function(lang) {
      switch (lang) {
        case "en":
          return "hi";
        case "es":
          return "hola";
        default:
          return "hello";
      }
    }
  };

  return obj;
};


/* and finally, use the object */
var name = "john", lang = "es";

var obj = newObj(name, lang);    

console.log(obj.name);
console.log(obj.greeter(lang));

As you returned property in the same way you can return function too;

\$\endgroup\$
0
-1
\$\begingroup\$

Yes it's a normal practice. Just be wary of function scope, functions declared outside of your object might not accessible.

\$\endgroup\$
4
  • \$\begingroup\$ Or did you mean Javascript objects instead like an associative arrays/hash that has key/value pairs? \$\endgroup\$ Apr 14 '16 at 9:14
  • \$\begingroup\$ yeah, javascript objects. thanks. is the code I posted okay to use in a real system? \$\endgroup\$
    – Yogesch
    Apr 14 '16 at 9:38
  • \$\begingroup\$ Yes, pretty much just be wary of function scope. Functions declared outside of your object might not accessible. \$\endgroup\$ Apr 14 '16 at 10:01
  • \$\begingroup\$ thanks. might i suggest to make this part of your answer since it answers the question directly. \$\endgroup\$
    – Yogesch
    Apr 14 '16 at 10:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.