The
console.log function is not actually a part of the JavaScript language itself.JavaScript Quick Tutorial / Complete Guide by Dinesh Deshmukh
console.log function is not actually a part of the JavaScript language itself.random() method.console.log(Math.random());
eval()uneval() isFinite()isNaN()parseFloat()parseInt()decodeURI()decodeURIComponent()encodeURI()encodeURIComponent()escape() unescape() ObjectFunctionBooleanSymbol ErrorEvalErrorInternalErrorRangeErrorReferenceErrorSyntaxErrorTypeErrorURIErrorArrayInt8ArrayUint8ArrayUint8ClampedArrayInt16ArrayUint16ArrayInt32ArrayUint32ArrayFloat32ArrayFloat64Arrayvar Person = function () {};
obj we use the statement new obj, assigning the result (which is of type obj) to a variable to access it later. An alternative way to create an new instance is to use Object.create. This will create an uninitialized instance.Person. In the example below we create two instances (person1 and person2).var person1 = new Person();
var person2 = new Person();
Object.create() method creates a new object with the specified prototype object and properties.Object.create(proto[, propertiesObject])
protopropertiesObjectTypeError exception if the proto parameter isn't null or an object.Object.createObject.create to achieve classical inheritance. This is for single inheritance, which is all that Javascript supports.// Shape - superclass
function Shape() {
this.x = 0;
this.y = 0;
}
// superclass method
Shape.prototype.move = function(x, y) {
this.x += x;
this.y += y;
console.info('Shape moved.');
};
// Rectangle - subclass
function Rectangle() {
Shape.call(this); // call super constructor.
}
// subclass extends superclass
Rectangle.prototype = Object.create(Shape.prototype);
Rectangle.prototype.constructor = Rectangle;
var rect = new Rectangle();
console.log("Is rect an instance of Rectangle? " + (rect instanceof Rectangle)); // true
console.log("Is rect an instance of Shape? " + (rect instanceof Shape)); // true
rect.move(1, 1); // Outputs, 'Shape moved.'
If you wish to inherit from multiple objects, then mixins are a possibility.function MyClass() {
SuperClass.call(this);
OtherSuperClass.call(this);
}
MyClass.prototype = Object.create(SuperClass.prototype); // inherit
mixin(MyClass.prototype, OtherSuperClass.prototype); // mixin
MyClass.prototype.myMethod = function() {
// do a thing
};
The mixin function would copy the functions from the
superclass prototype to the subclass prototype, the mixin function needs
to be supplied by the user. An example of a mixin like function would
be jQuery.extend.var Person = function () {};
// global namespace
var MYAPP = MYAPP || {};
Here in above code sample we have first checked whether MYAPP is
already defined (either in same file or in another file). If yes, then
use the existing MYAPP global object, otherwise create an empty object
called MYAPP which will encapsulate method, functions, variables and
objects.// sub namespace
MYAPP.event = {};
Below is code syntax for creating namespace and adding variable, function and method:// Create container called MYAPP.commonMethod for common method and properties
MYAPP.commonMethod = {
regExForName: "", // define regex for name validation
regExForPhone: "", // define regex for phone no validation
validateName: function(name){
// Do something with name, you can access regExForName variable
// using "this.regExForName"
},
validatePhoneNo: function(phoneNo){
// do something with phone number
}
}
// Object together with the method declarations
MYAPP.event = {
addListener: function(el, type, fn) {
// code stuff
},
removeListener: function(el, type, fn) {
// code stuff
},
getEvent: function(e) {
// code stuff
}
// Can add another method and properties
}
// Syntax for Using addListener method:
MYAPP.event.addListener("yourel", "type", callback);