Front End Developer (AngularJS) Question:
Download Job Interview Questions and Answers PDF
Explain how can you declare a class in Javascript?
Answer:
In 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.
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.
Download Front End Developer (AngularJS) Interview Questions And Answers
PDF
Previous Question | Next Question |
Tell me have you already used MVC before? What you like/dislike about it? | Please explain functions in CoffeeScript? |