Skip to content

Instantly share code, notes, and snippets.

@vienhoang
Created August 21, 2014 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vienhoang/46906604d532e7febdb8 to your computer and use it in GitHub Desktop.
Save vienhoang/46906604d532e7febdb8 to your computer and use it in GitHub Desktop.
JavaScript: Useful notes
JavaScript Notes
==========================================================
To declare an object
==========================================================
function Rectangle(w, h) {
this.width = w;
this.height = h;
};
==========================================================
Create a new instance of the object
==========================================================
var myRect = new Rectangle(100,150);
==========================================================
Methods in js. They are added to the class using the prototype object.
==========================================================
Rectangle.prototype.area = function() {
return this.width * this.height;
}
Rectangle.prototype.perimeter = function() {
return (2*this.width) + (2*this.height);
}
==========================================================
Objects inheritance
==========================================================
Step 1
function DepthRectangle(w,h,d) {
// Call func works like parent::__construct in PHP, call parent's constructor
Rectangle.call(this,w,h);
this.depth = d;
}
Step 2
Set the protype object of the subclass to be that of the superclass. Otherwise, we just got a subclass of Object which us not what we want.
DepthRectangle.prototype = new Rectangle();
Step 3
Since the DepthRectangle prototype was created with the Rectangle constructor, we have to re-assign the constructor property to be our DepthRectangle.
DepthRectangle.prototype.constructor = DepthRectangle;
Step 4
Define new methods or overwriting the parent's method.
DepthRectangle.prototype.volume = function() {
return (this.area() * this.depth);
}
DepthRectangle.prototype.toString = function() {
return "DepthRectangle with width: " + this.width + " & height: " + this.height + " & depth: " + this.depth;
}
==========================================================
Reg exp
==========================================================
| or
() group things together, can also define subexpressions
Match hello or good bye: /hello|good bye/
Match 4 digits or 5 characters: /\d{4}|\w{5}/
Match US postal code with optional 4 digits:
/^\d{5}(-\d{4})?$/
Match a phone number like this 123-4567
/\d{3}-\d{4}/
To be able to extract the individual portions use parentheses
Match a string with single or dubble quotes and anything in between * 0 or more times and match the same quote at the end. \1
/(['"])[^'"]*\1/
Match flags, putting outside the reg exp:
Case insensitive, match hello or HELLO, etc: /hello/i
Global search, match all "hello" strings: /hello/g
RegExp() object
RegExp.test() - Tests this RegExp against a give string
RegExp.exec() - Very powerful expression matching function
RegExp.ignoreCase - Same as i flag
RegExp.global - Same as g flag
RegExp.source - The read-only source string of the pattern
RegExp.lastIndex - The position index of the most recent match
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment