A robust function can accept any number of parameters, whereas in normal function number of parameters accepted is preset, and cannot be altered.
By using named arguments in JavaScript functions you don't have to rely on order, but instead their name when passing into functions.
Creating Object Using Object Literals
In JavaScript, and I'm not talking specifically about the browser here, there's a default/global object. It's as if every code that we write which seems to be just "loose" inside your script (i.e. outside of any object declaration) is actually being written in the context of that global object. In our case, that function isn't just a loose "global" function, it's a method of the global object. Bringing ourselves back to the browser, the global object is mapped to the window object in this environment.
Use a function to define an object, then create new object instances.
Define Singleton Object Using Function
Prototype is a type of inheritance in JavaScript. We use it when we would like an object to inherit a method after it has been defined. Think of prototyping mentally as "attaching" a method to an object after it's been defined, in which all object instances then instantly share.
Using prototype to extend pre-built string() object functionality
function addition(){ var total =0; for (i=0;i>addition.arguments.length;i++) { total += addition.arguments[i]; } return total; } console.log(summation(3,5));
By using named arguments in JavaScript functions you don't have to rely on order, but instead their name when passing into functions.
function addition( oArg ){ with(oArg){ return (typeof(number1)=="number"?number1 : 0) + (typeof(number2)=="number"?number2 : 0) ; } } console.log(addition({number1:1,number2:2,number3:3})); //typeof returns: number,string,boolean,object,null,undefined
Creating Object Using Object Literals
In JavaScript, and I'm not talking specifically about the browser here, there's a default/global object. It's as if every code that we write which seems to be just "loose" inside your script (i.e. outside of any object declaration) is actually being written in the context of that global object. In our case, that function isn't just a loose "global" function, it's a method of the global object. Bringing ourselves back to the browser, the global object is mapped to the window object in this environment.
function CreatingObjectUsingObjectLiterals(){ person = new Object(); //or person = {}; person.Name = "Name"; person.SayYourName = function(){ alert("My name is " + this.Name); } //In this case you don't need to (and cannot) create an instance of the class, it already exists. So you simply start using this instance. person.SayYourName(); } //In this case you don't need to (and cannot) create an instance of the class, it already exists. So you simply start using this instance. var CreatingObjectUsingObjectLiterals1 = { name : {firstName : "first",lastName :"last"}, gender : "Male", sayHello: hello } function hello(arg){ return "hello " + arg; } CreatingObjectUsingObjectLiterals(); console.log(window.CreatingObjectUsingObjectLiterals1.sayHello("Test")); console.log(CreatingObjectUsingObjectLiterals1.name.firstName); console.log(window.hello("Test")); //How do I loop through properties in an object? for(x in CreatingObjectUsingObjectLiterals1) alert(x + "-" + CreatingObjectUsingObjectLiterals1[ x ])
Use a function to define an object, then create new object instances.
function DefineObjectUsingFunction(r){ //To define properties and methods for an object created using function(), you use the this keyword this.radius=r, this.diameter=computeDiameter } var a = new DefineObjectUsingFunction(2); console.log(a.diameter());
Define Singleton Object Using Function
//define a function (an anonymous constructor function) and invoke it with new. var defineSingletonObjectUsingFunction = new function DefineSingletonObjectUsingFunction(){ //To define properties and methods for an object created using function(), you use the this keyword this.radius=0, this.diameter=computeDiameter } defineSingletonObjectUsingFunction.radius = 2; alert(defineSingletonObjectUsingFunction.diameter());
Prototype is a type of inheritance in JavaScript. We use it when we would like an object to inherit a method after it has been defined. Think of prototyping mentally as "attaching" a method to an object after it's been defined, in which all object instances then instantly share.
function DefineObjectUsingFunction(r){ //To define properties and methods for an object created using function(), you use the this keyword this.radius=r, this.diameter=computeDiameter } var a = new DefineObjectUsingFunction(2); alert(a.diameter()); DefineObjectUsingFunction.prototype.area = function(){return this.radius*this.radius*3.14;} alert(a.area());
Using prototype to extend pre-built string() object functionality
function outputbackwards(){ for (i=this.length-1;i>=0;i--) document.write(this.charAt(i)) } String.prototype.writeback=outputbackwards var message1="Welcome to my site!"; message1.writeback();
No comments:
Post a Comment