making the object oriented code in node.js

making the class circle: the name of the file will be circle.js

var PI = 3.14;

exports.area = function (r) {
  return PI * r * r;
};

exports.circumference = function (r) {
  return 2 * PI * r;
};

we can also use this in place of exports here .

for running the function of this class :

var circle = require('./circle');
console.log( 'The area of a circle of radius 4 is '+ circle.area(4));
console.log( 'The circumference of a circle of radius 4 is '+ circle.circumference(4));

we can also make a class like this

in circle .js

var Circle = function (radius) {
 this.radius = radius
}

Circle.PI = 3.14

Circle.prototype = {
 area: function () {
 return Circle.PI * this.radius * this.radius
 }
}

exports.Circle = Circle

and in foo.js

var circle = require('./oop')
var c = new circle.Circle(5)
console.log(c.area());

 

 

Advertisements

About rahul23134654

Hi, I am Rahul Meha , B.E. in (I.T.)
This entry was posted in Node.js. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s