In JavaScript, most things are objects. An object is a collection of related data and/or functionality
Namespace: Everything you create in JavaScript is by default global. In JavaScript namespace is Global object.
How to create a namespace in JavaScript?
Create one global object, and all variables, methods, and functions become properties of that object.
Example:
// global namespace
var MYAPP = MYAPP || {};
Explaination: Here we first checked whether MYAPP is already defined.If yes, then use the existing MYAPP global object, otherwise create an empty object called MYAPP
How to create sub namespaces?
// sub namespace
MYAPP.event = {};
Class
JavaScript is a prototype-based language and contains no class statement.JavaScript classes create simple objects and deal with inheritance.
Example:
var Person = function () {};
Explaination: Here we define a new class called Person with an empty constructor.
Use the class keyword
class Calculation {};
A class expression is another way to define a class. Class expressions can be named or unnamed.
// unnamed
var Calculation = class {};
// named
var Calculation = class Calculation { };
Function based classes
function Calculation() { }
// associate method to class
Calculation.prototype.square = function() {
return this;
}
Calculation.cube = function() {
return this;
}
let obj = new Calculation();
let square = obj.square;
square(); // global object
let cube = Calculation.cube;
cube(); // global object
Sub classing with extends
class Teacher {
constructor(name) {
this.name = name;
}
attendance() {
console.log(this.name + ' teacher present.');
}
}
class Student extends Teacher {
attendance() {
console.log(this.name + ' student present.');
}
}
var d = new Student('Sudhir');
d.attendance(); // Sudhir student present.
JavaScript object
A JavaScript object is a collection of named values
var address = {city:"Noida", state:"Uttar Pradesh", country:"India"};
To access city use : address.city;
Or you can create using new keyword
var address = new Object();
address.city = "Noida";
address.state = "Uttar Pradesh";
address.country = "India";
Note:JavaScript objects are mutable.Any changes to a copy of an object will also change the original.Every object in JavaScript is an instance of the object Object and therefore inherits all its properties and methods.
Example:
var address = {city:"Noida", state:"Uttar Pradesh", country:"India"}
var x = address;
x.state = "Delhi";
document.getElementById("demo").innerHTML =
address.city + " is " + address.state + " state.";
Constructor
In JavaScript the function serves as the constructor of the object, therefore there is no need to explicitly define a constructor method.
Example:
var Person = function () {
console.log('instance created');
};
var person1 = new Person();
Example:
function Person(name) {
this.name = name;
}
var thePerson = new Person('Redwood');
console.log('thePerson.constructor is ' + thePerson.constructor);
This example displays the following output:
thePerson.constructor is function Tree(name) {
this.name = name;
}
The methods
To define a method, assign a function to a named property of the class's prototype property.
Example:
var Person = function (firstName) {
this.firstName = firstName;
};
Person.prototype.sayHello = function() {
console.log("Hello, I'm " + this.firstName);
};
var person1 = new Person("Alice");
person1.sayHello();
var helloFunction = person1.sayHello;
helloFunction.call(person1);
For loop in JavaScript
var address = {fname:"Noida", state:"Uttar Pradesh", country:"India"};
for (x in address) {
txt += address[x];
}
JavaScript object method:
var address = {
city: "Noida",
state:"Uttar Pradesh",
country:"India"
fullAddress : function() {
return this.city + " " + this.state + " " + this.country;
}
};
address.fullAddress(); // returns fullAddress
JavaScript Prototypes
A prototype is an object from which other objects inherit properties.Every object has a prototype by default.
Adding Methods to a Prototype
Example:
function Address(city, state) {
this.city = city;
this.state = last;
this.cityState = function() {
return this.city + " " + this.state
};
}
Address.prototype.location = function() {
return this.city*this.state;
}
var myAddress = new Address("Noida", "Delhi");
myAddress.cityState();
myAddress.location(); // It will return Noida Delhi
Using the prototype Property
JavaScript prototype property allows you to add new properties to an existing prototype
Example: Address.prototype.nationality = "English";
Place the javascript function inside a variable
var consoleOutput = function consoleOutput()
{
console.log('Hello Sudhir');
}
Bind a function with event javascript
document.getElementsByClassName("inner-heading")[0].addEventListener("click", consoleOutput);
Same thing can be done by using jquery
$(".inner-heading").click(consoleOutput);
Lets us take real example for bootstrap notification
var flashMessage;
flashMessage = flashMessage || (function(){
var loadingDiv = $('<div id="myModal" class="modal fade" role="dialog"><div class="modal-dialog"><div class="modal-content"><div class="modal-body"><i class="fa fa-spin fa-spinner fa-5x"></i></div></div></div></div>');
return {
showPleaseWait: function() {
loadingDiv.modal('show');
loadingDiv.on('hidden.bs.modal', function(){
loadingDiv.remove();
});
},
hidePleaseWait: function () {
loadingDiv.modal('hide');
},
alertMessageBox:function(msg,type){
if ($('section').hasClass('_alertDialog')) {
$('._alertDialog').remove();
}
if(type == 'success'){
_curAlertClass = 'alert-success';
_curAlertIcon = 'fa-check';
}else{
_curAlertClass = 'alert-danger';
_curAlertIcon = 'fa-warning';
}
var _dialogContainer = $('<section class="content _alertDialog" style="display:none;"></section>');
var _ele = $('<div class="alert '+ _curAlertClass +' alert-dismissable"><a href="#" class="close" data-dismiss="alert" aria-label="close">×</a><div class="alert-message"><i class="fa '+_curAlertIcon+'"></i> '+msg+'</div></div>');
_dialogContainer.html(_ele);
$('body').after(_dialogContainer);
_dialogContainer.fadeIn(400);
var _timerId = setTimeout(function() { _dialogContainer.slideUp( 300 ); clearTimeout(_timerId); }, 5000);
}
}
})();
Now How to access its function
flashMessage.showPleaseWait();
flashMessage.hidePleaseWait();
flashMessage.alertMessageBox('<p>This is testing javascript object</p>', 'success');
Namespace: Everything you create in JavaScript is by default global. In JavaScript namespace is Global object.
How to create a namespace in JavaScript?
Create one global object, and all variables, methods, and functions become properties of that object.
Example:
// global namespace
var MYAPP = MYAPP || {};
Explaination: Here we first checked whether MYAPP is already defined.If yes, then use the existing MYAPP global object, otherwise create an empty object called MYAPP
How to create sub namespaces?
// sub namespace
MYAPP.event = {};
Class
JavaScript is a prototype-based language and contains no class statement.JavaScript classes create simple objects and deal with inheritance.
Example:
var Person = function () {};
Explaination: Here we define a new class called Person with an empty constructor.
Use the class keyword
class Calculation {};
A class expression is another way to define a class. Class expressions can be named or unnamed.
// unnamed
var Calculation = class {};
// named
var Calculation = class Calculation { };
Function based classes
function Calculation() { }
// associate method to class
Calculation.prototype.square = function() {
return this;
}
Calculation.cube = function() {
return this;
}
let obj = new Calculation();
let square = obj.square;
square(); // global object
let cube = Calculation.cube;
cube(); // global object
Sub classing with extends
class Teacher {
constructor(name) {
this.name = name;
}
attendance() {
console.log(this.name + ' teacher present.');
}
}
class Student extends Teacher {
attendance() {
console.log(this.name + ' student present.');
}
}
var d = new Student('Sudhir');
d.attendance(); // Sudhir student present.
JavaScript object
A JavaScript object is a collection of named values
var address = {city:"Noida", state:"Uttar Pradesh", country:"India"};
To access city use : address.city;
Or you can create using new keyword
var address = new Object();
address.city = "Noida";
address.state = "Uttar Pradesh";
address.country = "India";
Note:JavaScript objects are mutable.Any changes to a copy of an object will also change the original.Every object in JavaScript is an instance of the object Object and therefore inherits all its properties and methods.
Example:
var address = {city:"Noida", state:"Uttar Pradesh", country:"India"}
var x = address;
x.state = "Delhi";
document.getElementById("demo").innerHTML =
address.city + " is " + address.state + " state.";
Constructor
In JavaScript the function serves as the constructor of the object, therefore there is no need to explicitly define a constructor method.
Example:
var Person = function () {
console.log('instance created');
};
var person1 = new Person();
Example:
function Person(name) {
this.name = name;
}
var thePerson = new Person('Redwood');
console.log('thePerson.constructor is ' + thePerson.constructor);
This example displays the following output:
thePerson.constructor is function Tree(name) {
this.name = name;
}
The methods
To define a method, assign a function to a named property of the class's prototype property.
Example:
var Person = function (firstName) {
this.firstName = firstName;
};
Person.prototype.sayHello = function() {
console.log("Hello, I'm " + this.firstName);
};
var person1 = new Person("Alice");
person1.sayHello();
var helloFunction = person1.sayHello;
helloFunction.call(person1);
For loop in JavaScript
var address = {fname:"Noida", state:"Uttar Pradesh", country:"India"};
for (x in address) {
txt += address[x];
}
JavaScript object method:
var address = {
city: "Noida",
state:"Uttar Pradesh",
country:"India"
fullAddress : function() {
return this.city + " " + this.state + " " + this.country;
}
};
address.fullAddress(); // returns fullAddress
JavaScript Prototypes
A prototype is an object from which other objects inherit properties.Every object has a prototype by default.
Adding Methods to a Prototype
Example:
function Address(city, state) {
this.city = city;
this.state = last;
this.cityState = function() {
return this.city + " " + this.state
};
}
Address.prototype.location = function() {
return this.city*this.state;
}
var myAddress = new Address("Noida", "Delhi");
myAddress.cityState();
myAddress.location(); // It will return Noida Delhi
Using the prototype Property
JavaScript prototype property allows you to add new properties to an existing prototype
Example: Address.prototype.nationality = "English";
Place the javascript function inside a variable
var consoleOutput = function consoleOutput()
{
console.log('Hello Sudhir');
}
Bind a function with event javascript
document.getElementsByClassName("inner-heading")[0].addEventListener("click", consoleOutput);
Same thing can be done by using jquery
$(".inner-heading").click(consoleOutput);
Lets us take real example for bootstrap notification
var flashMessage;
flashMessage = flashMessage || (function(){
var loadingDiv = $('<div id="myModal" class="modal fade" role="dialog"><div class="modal-dialog"><div class="modal-content"><div class="modal-body"><i class="fa fa-spin fa-spinner fa-5x"></i></div></div></div></div>');
return {
showPleaseWait: function() {
loadingDiv.modal('show');
loadingDiv.on('hidden.bs.modal', function(){
loadingDiv.remove();
});
},
hidePleaseWait: function () {
loadingDiv.modal('hide');
},
alertMessageBox:function(msg,type){
if ($('section').hasClass('_alertDialog')) {
$('._alertDialog').remove();
}
if(type == 'success'){
_curAlertClass = 'alert-success';
_curAlertIcon = 'fa-check';
}else{
_curAlertClass = 'alert-danger';
_curAlertIcon = 'fa-warning';
}
var _dialogContainer = $('<section class="content _alertDialog" style="display:none;"></section>');
var _ele = $('<div class="alert '+ _curAlertClass +' alert-dismissable"><a href="#" class="close" data-dismiss="alert" aria-label="close">×</a><div class="alert-message"><i class="fa '+_curAlertIcon+'"></i> '+msg+'</div></div>');
_dialogContainer.html(_ele);
$('body').after(_dialogContainer);
_dialogContainer.fadeIn(400);
var _timerId = setTimeout(function() { _dialogContainer.slideUp( 300 ); clearTimeout(_timerId); }, 5000);
}
}
})();
Now How to access its function
flashMessage.showPleaseWait();
flashMessage.hidePleaseWait();
flashMessage.alertMessageBox('<p>This is testing javascript object</p>', 'success');
Sap Training Institute in Noida-Webtrackker is the best SAP training institute in noida
ReplyDeleteSas Training Institute in Noida
PHP Training Institute in Noida
Hadoop Training Institute in Noida
Oracle Training Institute in Noida
Dot net Training Institute in Noida
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteAustralia best tutor is well known academic portal. Here students can get different kind of Online Assignment help services like that
ReplyDelete1.Online Assignment Help
2.Instant Assignment Help
3.Assignment Help
4.Help with Assignment
5.my assignment Help
And also access that services at any time and any where.
Thank you for this post. Thats all I are able to say. You most absolutely have built this blog website into something speciel. You clearly know what you are working on, youve insured so many corners.thanks
ReplyDeleteSelenium training in Chennai
Selenium training in Bangalore
I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing..
ReplyDeleteBelieve me I did wrote an post about tutorials for beginners with reference of your blog.
Selenium training in bangalore
Selenium training in Chennai
Selenium training in Bangalore
Selenium training in Pune
Selenium Online training
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteAmazing post. I really enjoy for this status.
ReplyDeleteThis post are very useful and powerful . Your services are best.
Thanks for sharing..Study in Canada Consultants
study in Canada consultansts in Delhi
canada immigration consultants in delhi
I am very Interesting to Read all your Articles...Your services are best
ReplyDeleteJava training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery
This comment has been removed by the author.
ReplyDeleteThanks for sharing the valuable information.
ReplyDeleteOnline Training
software training institute
online classes
Simple and interesting blog.Keep blogging.
ReplyDeleteJava training in Chennai
Java training in Bangalore
Java training in Hyderabad
Java Training in Coimbatore
Java Online Training
such an useful blog.Java training in Chennai
ReplyDeleteJava training in Bangalore
Java training in Hyderabad
Java Training in Coimbatore
Java Online Training
Thanks for the informative article. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing.
ReplyDeletehardware and networking training in chennai
hardware and networking training in tambaram
xamarin training in chennai
xamarin training in tambaram
ios training in chennai
ios training in tambaram
iot training in chennai
iot training in tambaram
Thanks a lot very much for the high your blog post quality and results-oriented help. I won’t think twice to endorse to anybody who wants and needs support about this area.
ReplyDeleteangular js training in chennai
angular js training in velachery
full stack training in chennai
full stack training in velachery
php training in chennai
php training in velachery
photoshop training in chennai
photoshop training in velachery
I am really happy with your blog because your article is very unique and powerful for new reader.
ReplyDeletehadoop training in chennai
hadoop training in annanagar
salesforce training in chennai
salesforce training in annanagar
c and c plus plus course in chennai
c and c plus plus course in annanagar
machine learning training in chennai
machine learning training in annanagar
Thanks for sharing such a nice info.I hope you will share more information like this. please keep on sharing!
ReplyDeletedata science training in chennai
data science training in omr
android training in chennai
android training in omr
devops training in chennai
devops training in omr
artificial intelligence training in chennai
artificial intelligence training in omr
Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
ReplyDeletesplunk online training
Thanks for sharing valuable information.
ReplyDeleteMulesoft training in hyderabad
https://www.chihuahuapuppiesforsale1.com/
ReplyDeletehttps://www.chihuahuapuppiesforsale1.com/chihuahua-puppies-for-sale-near-me/
https://www.chihuahuapuppiesforsale1.com/teacup-chihuahuas-puppies-for-sale/
https://www.chihuahuapuppiesforsale1.com/chihuahuas-for-sale/
https://www.chihuahuapuppiesforsale1.com/teacup-chihuahuas-for-sale/
https://www.chihuahuapuppiesforsale1.com/chihuahua-pupies-for-sale/
https://www.chihuahuapuppiesforsale1.com/chihuahua-puppies-near-me/
https://www.chihuahuapuppiesforsale1.com/chihuahua-for-sale/
https://www.chihuahuapuppiesforsale1.com/teacup-chihuahua-puppies-for-sale-2/
This post is very simple to read and appreciate without leaving any details out. Great work!
ReplyDeletedata analytics training in aurangabad
smm panel
ReplyDeletesmm panel
iş ilanları
İnstagram takipçi satın al
Hirdavatciburada.com
WWW.BEYAZESYATEKNÄ°KSERVÄ°SÄ°.COM.TR
servis
Tiktok para hilesi indir
kadıköy arçelik klima servisi
ReplyDeletetuzla vestel klima servisi
tuzla bosch klima servisi
ataÅŸehir mitsubishi klima servisi
kadıköy vestel klima servisi
kartal samsung klima servisi
ümraniye samsung klima servisi
üsküdar vestel klima servisi
beykoz bosch klima servisi
Windows 7 product keys for 64-bit version · MLPOK-NJIUH-BVGYT-FCXDR-ESZAQ · W1Q2A-3S4F4-R5TGY-HG7UH-Y8IKJ · M9N8B-7V6C5-X4Z32-SDA4D-EF5GH · T6HJY-67JKI-U789L-KMNBV- .Windows 7 Product Key Crack
ReplyDeleteSynthesia Crack is a best tool to learn or play the piano in an advanced manner. It works like a real piano to provide you the full taste. File Magic License Key
ReplyDelete