Prototype-based programming
Prototype-based programming is an OOP model it will not uses Classes but rather it first accomplishes the behavior of any class and then reuse it.
the class-less programming style grows increasingly popular lately, and has been adopted for programming languages such as JavaScript,
Namespace
A namespace is a container which allows developers to bundle up functionality under a unique, application-specific name. In JavaScript a namespace is just another object containing methods, properties, and objects.
we can create global namespace
var FirstAPP = FirstAPP || {};
we can create sub namespaces also like as fllowing
Firstapp.handle={};
we can create and add variable and fuctions to the name space also the following syntax will make you easily understand the syntax for adding elements to name space.
FirstAPP.basic= {
Name: "", // define regex for name validation
mobile: "", // define regex for phone no validation
email:"",
validateMobile: function(number){
},
validateEmail: function(phoneNo){
}
}
There are plenty Built-in objects for example, there are objects like following
String, Number, Operators, Statements, Math, Date, Array, Boolean, RegExp, Global, Conversion..etc..
Every object in JavaScript is an instance of the object Object and therefore inherits all its properties and methods.
Except these we can create our own Custom objects.
Custom objects
JavaScript uses functions as classes. Defining a class is as easy as defining a function. In the example below we define a new class called Person.
var info= function{} //so here we have creates a class
Now we can create two instances(objects) for that class
var info1=new info();
var info2=new info();
The constructor
The constructor is called at the moment when the object instance is created. The constructor is a method of the class. In JavaScript the function serves as the constructor of the object, therefore there is no need to explicitly define a constructor method. Every action declared in the class gets executed at the time of instantiation.
Example
<script>
var i=0;
var Person = function () {
document.getElementById("p1").innerHTML="instance"+i+"created";
i++;
};
setTimeOut("fun1",1000)
function fun1(){
var person1 = new Person();
setTimeOut("fun2",1000)
}
function fun2(){
var person2 = new Person();
}
</script>
property (object attribute)
Properties are variables contained in the class; every instance of the object has those properties. Properties are set in the constructor (function) of the class so that they are created on each instance.
In the example below, we define the naame property for the Person class at
<script>
var i=0;
var Person = function (name) {
this.name=name;
if(i==0){
document.getElementById("p1").innerHTML=this.name;
}else{
document.getElementById("p2").innerHTML=this.name;
document.getElementById("p1").style.color="green";
}
i++;
};
var person1 = new Person("raj");
setTimeout('fun()',2000);
function fun(){
var person2 = new Person("sharuk");
}
</script>
in the above code the instance person2 will call after 2seconds...
In the next post i will continue this concept....
Thank you for visiting...please leave your comments and doubts
Prototype-based programming is an OOP model it will not uses Classes but rather it first accomplishes the behavior of any class and then reuse it.
the class-less programming style grows increasingly popular lately, and has been adopted for programming languages such as JavaScript,
Namespace
A namespace is a container which allows developers to bundle up functionality under a unique, application-specific name. In JavaScript a namespace is just another object containing methods, properties, and objects.
we can create global namespace
var FirstAPP = FirstAPP || {};
we can create sub namespaces also like as fllowing
Firstapp.handle={};
we can create and add variable and fuctions to the name space also the following syntax will make you easily understand the syntax for adding elements to name space.
FirstAPP.basic= {
Name: "", // define regex for name validation
mobile: "", // define regex for phone no validation
email:"",
validateMobile: function(number){
},
validateEmail: function(phoneNo){
}
}
There are plenty Built-in objects for example, there are objects like following
String, Number, Operators, Statements, Math, Date, Array, Boolean, RegExp, Global, Conversion..etc..
Every object in JavaScript is an instance of the object Object and therefore inherits all its properties and methods.
Except these we can create our own Custom objects.
Custom objects
JavaScript uses functions as classes. Defining a class is as easy as defining a function. In the example below we define a new class called Person.
var info= function{} //so here we have creates a class
Now we can create two instances(objects) for that class
var info1=new info();
var info2=new info();
The constructor
The constructor is called at the moment when the object instance is created. The constructor is a method of the class. In JavaScript the function serves as the constructor of the object, therefore there is no need to explicitly define a constructor method. Every action declared in the class gets executed at the time of instantiation.
Example
<script>
var i=0;
var Person = function () {
document.getElementById("p1").innerHTML="instance"+i+"created";
i++;
};
setTimeOut("fun1",1000)
function fun1(){
var person1 = new Person();
setTimeOut("fun2",1000)
}
function fun2(){
var person2 = new Person();
}
</script>
property (object attribute)
Properties are variables contained in the class; every instance of the object has those properties. Properties are set in the constructor (function) of the class so that they are created on each instance.
In the example below, we define the naame property for the Person class at
<script>
var i=0;
var Person = function (name) {
this.name=name;
if(i==0){
document.getElementById("p1").innerHTML=this.name;
}else{
document.getElementById("p2").innerHTML=this.name;
document.getElementById("p1").style.color="green";
}
i++;
};
var person1 = new Person("raj");
setTimeout('fun()',2000);
function fun(){
var person2 = new Person("sharuk");
}
</script>
in the above code the instance person2 will call after 2seconds...
In the next post i will continue this concept....
Thank you for visiting...please leave your comments and doubts
No comments:
Post a Comment