If you do that you probably lose some of the performance enhancements V8
does by converting things internally to classes.

On Sat, Jan 10, 2015 at 8:39 PM, Alexander Praetorius <[email protected]>
wrote:

> Many people use *"this"*, *"prototype"* and *"new"*, like:
>
>
>> *module.exports = function Car (name, cost, mpg, alternative) {*
>> *  this.name <http://this.name> = name;*
>> *  this.cost = cost;*
>> *  this.env_damage = mpg;*
>> *  this.alternative  = alternative;*
>> *};*
>> *Car.prototype.appeal = function() {*
>> *  return(this.cost/this.mpg);*
>> *};*
>> *Car.prototype.snark = function() {*
>> *  return(this.alternative.advise());**};*
>
>
> I think that is very "beginner unfriendly" and a lot of people i know got
> very confused by
> *"prototypes"* and the different way* "this"* can be set and how it can
> trip you over.
> And i remember a time when i wanted to transform an instance after
> creation, but
> had no influence over all the many places which used "new Name(...)", so i
> kinf of
> dislike the use of *"new"* too.
> I wonder if it would be bad to get rid of those and instead write the car
> module like:
>
>
>
>> *module.exports = function Car (name, cost, mpg, alternative) {*
>> *  var api = {*
>> *    name: name,*
>> *    cost: cost,*
>> *    env_damage: mpg,*
>> *    alternative: alternative,*
>> *    appeal: function appeal () { api.cost / api.mpg; },*
>> *    snark: function snark  () { api.alternative.advise(); }*
>> *  };*
>> *  return api;**};*
>
>
>
>
> 2015-01-10 17:12 GMT+01:00 // ravi <[email protected]>:
>
>> On Jan 10, 2015, at 5:04 AM, Prabhash Rathore <[email protected]>
>> wrote:
>> >
>> > Based on your replies, I think this is the best way to represent a data
>> object as it's just data and object can represent this data:
>> > module.exports = {
>> >
>> >     name
>> > : '';
>> >
>> >     department
>> > : '';
>> > }
>> > However I have noticed people using function() to create data objects
>> and then they just return an object inside the function (see below
>> example)? Is there any specific use case or reason on using function
>> instead of using just object literal?
>>
>>
>>
>> It depends on what it is you want your module to offer. Say the module
>> provides a bunch of utility functions. Then you may just choose to export
>> each function and be done:
>>
>> str_utils.js:
>>
>> exports.uncamelcase   = function(str) { …. };
>> exports.is_palindrome = function(str) { …. };
>> exports.is_numeric    = function(str) { …. };
>>
>> Then someone can use it thus:
>>
>> var str_utils = require(‘str_utils’);
>>
>> if( str_utils.is_palindrome(person_name) === true )
>>     console.log(“Well, aren’t you special!”);
>>
>> On the other hand, if your module returns a constructor/class to be used
>> for instantiating new objects, then you may wish to do this:
>>
>> module.exports = Car;
>>
>> function Car(name, cost, mpg, alternative)
>> {
>>     this.name = name;
>>     this.cost = cost;
>>     this.env_damage = mpg;
>>     this.alternative = alternative;
>> }
>>
>> Car.prototype.appeal =
>> function()
>> {
>>     return(this.cost/this.mpg);
>> };
>>
>> Car.prototype.snark =
>> function()
>> {
>>     return(this.alternative.advise());
>> };
>>
>> Then someone could:
>>
>> var Car = require(‘car’);
>> var metro = require(‘metro’);
>>
>> var new_car = new Car(‘Hummer’, 80000, 2, metro);
>>
>> if( new_car.appeal() > 700 )
>>     console.log(“Congratulations on your fancy”, new_car.name, “but”,
>> new_car.snark());
>>
>> Etc. Keep in mind the part played by require/module caching (if you
>> return an object, it will be cached and returned from the cache at the next
>> require — this may or may not be what you want: I use it as a way to load
>> dependencies without passing them around).
>>
>>         —ravi
>>
>>
>> --
>> Job board: http://jobs.nodejs.org/
>> New group rules:
>> https://gist.github.com/othiym23/9886289#file-moderation-policy-md
>> Old group rules:
>> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "nodejs" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> To post to this group, send email to [email protected].
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/nodejs/6CC0F73D-A104-487B-83F6-0930105B236C%40g8o.net
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>  --
> Job board: http://jobs.nodejs.org/
> New group rules:
> https://gist.github.com/othiym23/9886289#file-moderation-policy-md
> Old group rules:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> ---
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/nodejs/CAN5%2BLUuVpyXqF-SfGjBF_WuRR5LqN4LTAZQ77%3DseQjtiAY-VmQ%40mail.gmail.com
> <https://groups.google.com/d/msgid/nodejs/CAN5%2BLUuVpyXqF-SfGjBF_WuRR5LqN4LTAZQ77%3DseQjtiAY-VmQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/CAPJ5V2ZLG9FzH2GK%2BxxGn6c8-bXFZR8iDWVyNbh0RXEhR_qy7g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to