Interview Questions Answers.ORG
Interviewer And Interviewee Guide
Interviews
Quizzes
Home
Quizzes
Interviews Scripting language Interviews:AngularJSAngularJS DeveloperChrome FrameDojoExpert Developer JavaScriptExpert JavaScript DeveloperExt CoreEXT-GWTExt-JSFront End Developer (AngularJS)JQuery DeveloperjQuery MobileJQuery ProgrammerJQuery UIMooToolsPrototype FrameworkQooxdooScriptingSencha TouchSizzle Selector EngineSWFObjectWeb Font LoaderXMLHttpRequest
Copyright © 2018. All Rights Reserved
Front End Developer (AngularJS) Interview Question:
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
Copyright 2007-2024 by Interview Questions Answers .ORG All Rights Reserved.
https://InterviewQuestionsAnswers.ORG.
https://InterviewQuestionsAnswers.ORG.