+ 2
In java script, their is no class but we do have constructors and we can create objects from that. How is it actually happening?
3 Antworten
+ 3
//contructor syntax
function Classname() {
// methods goes here
this.exampleMethod = function() { // method code};
}// end of contructor
//creating objects using constructor above
var newObject = new Classname();
var anotherObject = new Classname();
+ 2
i think u have different questions:
where are the variables declared, those which are called by the constructor?
when u use the "this. " inside the constructor and type a name, you are automatically declaring that as a new variable that belongs to that object. javascript does that, no need to use the var word.
can we call the object thats called by the constructor?
yeah!
function Object() {
this.propertyName = 10;
this.methodName = function() {//method stuff};
}
// creating objects and calling their properties
var thing = new Object();
thing.Propertyname =5;
thing.MethodName();
0
True that we can use new keyword to call the constructor , so constructor is initializing the variables using this keyword, but there is no concept of class so where are these variables declared and can we call the object as it's an object of a class?