Explain how can you declare a class in Javascript?
Submitted by: MuhammadIn javascript there's no classes like in Java, what we actually call a class is in reality a function simulating a class behaviour. For being so flexible, there are many ways to create a class in javascript, below you'll find 3 ways of doing that.
Class using function as a constructor:
function Person(name) {
this.name = name;
}
// Creating an object
var person = new Person("Hussain");
person.name; // "Hussain"
It's very important to notice that you have to use the keyword new when creating a new instance of that class otherwise you will have logical problems regarding the this will reference window object.
Class Literal notation:
var person = {
name: "",
setName: function(name) {
this.name = name;
}
}
person.setName("Hussain");
person.name; // "Hussain"
In this example we don't use a function to define our class, we are creating a singleton object person with one attribute and one method. You can use that object straightaway, no instantiation in this case.
That notation is useful when you don't need to create instances of that class or you'll use it just once in your application.
Singleton through a function:
var person = new function() {
this.setName = function(name) {
this.name = name;
}
this.sayHi = function() {
return "Hi, my name is " + this.name;
}
}
person.setName("Hussain");
alert(person.sayHi()); // Hi, my name is Hussain
As you can see in the code snippet above, we have a function like the first example and besides we also have the new keyword before the function declaration. It means that we are creating one instance of that class at the same time we are declaring it.
Submitted by: Muhammad
Class using function as a constructor:
function Person(name) {
this.name = name;
}
// Creating an object
var person = new Person("Hussain");
person.name; // "Hussain"
It's very important to notice that you have to use the keyword new when creating a new instance of that class otherwise you will have logical problems regarding the this will reference window object.
Class Literal notation:
var person = {
name: "",
setName: function(name) {
this.name = name;
}
}
person.setName("Hussain");
person.name; // "Hussain"
In this example we don't use a function to define our class, we are creating a singleton object person with one attribute and one method. You can use that object straightaway, no instantiation in this case.
That notation is useful when you don't need to create instances of that class or you'll use it just once in your application.
Singleton through a function:
var person = new function() {
this.setName = function(name) {
this.name = name;
}
this.sayHi = function() {
return "Hi, my name is " + this.name;
}
}
person.setName("Hussain");
alert(person.sayHi()); // Hi, my name is Hussain
As you can see in the code snippet above, we have a function like the first example and besides we also have the new keyword before the function declaration. It means that we are creating one instance of that class at the same time we are declaring it.
Submitted by: Muhammad
Read Online Front End Developer (AngularJS) Job Interview Questions And Answers
Top Front End Developer (AngularJS) Questions
☺ | What is AngularJS? What are the key features of AngularJS? Is there any down-side of using AngularJS? |
☺ | What is an IIFE? |
☺ | Explain me your workflow when you create a web page? |
☺ | How can you add a method to a class already defined? |
☺ | Why is this project called "AngularJS"? Why is the namespace called "ng"? |
Top Scripting language Categories
☺ | AngularJS Interview Questions. |
☺ | Ext-JS Interview Questions. |
☺ | Dojo Interview Questions. |
☺ | Expert Developer JavaScript Interview Questions. |
☺ | jQuery Mobile Interview Questions. |