Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/eftakhairul/nodejs-tricks
Bringing all the helpful tricks in one place
https://github.com/eftakhairul/nodejs-tricks
Last synced: about 1 month ago
JSON representation
Bringing all the helpful tricks in one place
- Host: GitHub
- URL: https://github.com/eftakhairul/nodejs-tricks
- Owner: eftakhairul
- License: apache-2.0
- Created: 2015-11-09T03:39:00.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2015-12-24T21:54:53.000Z (about 9 years ago)
- Last Synced: 2024-04-09T15:07:11.339Z (9 months ago)
- Language: JavaScript
- Size: 13.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
#NodeJS Tricks
Bringing all the helpful tricks from the different websites, talks, tweets etc in one place. Just fork and contribute this one.##Table of Contents
- [Inheritance](#inheritance)##Tricks
###Inheritance
```javascript
'use strict';//Classic OOP
var Rectangle = function (length, width) {
this.length = length;
this.width = width;//Methods defined internally
this.calcArea = function() {
return this.length * this.width;
};
};var Square = function (length) {
this.length = length;
this.width = length;
};//For Inheriting all methods
Square.prototype = new Rectangle();
//OR
Square.prototype = Object.create(Rectangle.prototype);//Creating instance of Square
var mySquare = new Square(2);
console.log(mySquare.calcArea());//Result:
4//Modular Inheritance
//Base.js
function Base() {}
Base.prototype.print = function() {
Console.log('Printing.');
};module.exports = Base;
//User.js
var util = require('util');
var Base = require('relative/or/absolute/path/to/Base');function User() {}
util.inherits(User, Base);
module.exports = User;User.prototype.display = function() {
Console.log('Displaying.');
};var User = require('relative/or/absolute/path/to/User');
var userObj = new User();userObj.print(); //Printing.
userObj.display(); //Displaying.'```
[View Source](inheritance.js)##Contributing
1. Fork it
1. Create your trick branch: `git checkout -b my-js-trick
1. Add your trick to the collection of `.js` files
1. Regenerate `README.md`: `gulp build` (Install dependencies: npm install)
1. Commit your changes: `git commit -am 'Add trick'`
1. Push to the branch: `git push origin my-js-trick`
1. Create new Pull Request and explain why your code is trick