You must write

this.getDistance = ...

instead of

getDistance = ...

without this.




On Thu, Mar 27, 2014 at 8:02 AM, fm3391 <[email protected]> wrote:

> I tried this and everything looks better. The class should only be
> instantiated once from server.js.  I think my main problem is I don't know
> how to make a method to return the value! Please help!
>
> On Thursday, March 27, 2014 5:05:17 AM UTC-5, ryandesign wrote:
>
>>
>> On Mar 27, 2014, at 04:53, fm3391 <[email protected]> wrote:
>>
>> > contents of "sensor1_class.js"
>> >
>> > var Sensor1 = function(){
>> >         this.distance = 0;
>> >
>> >         getDistance: function (){
>> >                 return distance;
>> >         }
>> >
>> > };
>> >
>> > module.exports = Sensor1;
>> >
>> >
>> >
>> >
>> > partial contents of "server.js"
>> >
>> > var sensor1 = require('./sensor1_class');
>> >
>> > console.log(sensor1.getDistance()); <--- Here is where I am having the
>> trouble, I just want to call a method of object "Sensor1" that returns the
>> property "distance"
>>
>> You have exported the class constructor function; you haven't called the
>> function anywhere to instantiate it.
>>
>> If your intention is that multiple objects could be created, then you've
>> exported the right thing, and you need to call that function when you
>> require it, e.g.:
>>
>>
>> var sensor1_class = require('./sensor1_class');
>> var sensor1 = sensor1_class();
>>
>>
>> Alternately, if it is your intention that there be a single instance of
>> this object that every other file that requires the class shares, then you
>> should instantiate the object in sensor1_class.js and return that instance:
>>
>>
>> module.exports = Sensor1();
>>
>>
>>  --
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> 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 post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>
> ---
> 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].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
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 post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
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].
For more options, visit https://groups.google.com/d/optout.

Reply via email to