-2

Formed an object 'myclass' in my js file like this:

var myclass = function(){
  this.m1 = function(){
    return "THis is m1 of myclass"
  };
  return "this is myclass";
}
myclass.prototype.mp1 = function(){
   return "this is mp1 of myclass"
}

now i do this:

var a = new myclass();
var b = new myclass;

Q1- I dont see any difference between the behaviors of these two. Please explain the reason.

Q2 - When I do - console.log(a), i expect to get "this is myclass" in console, but instead I see [object object].

Q3 - If I return an object from the constructor of 'myclass'(eg - return {x:1}), then I cannot use 'm1' or 'mp1' anymore from the instances of 'myclass'.

Q4 - Comparing this with Date class of window object, this is totally different behavior. If a = new Date(), then i see value of 'a' as a date although the type is an object, and this points to my next question.

Q5 - The date class returns a date which looks like a string in DOM but the 'typeof' of the instance of date class is an object. Why's that?

Sorry for posting so many questions at once, but it would be nice if I get answers for all questions as I think they are related.

This question is not about a particular question from the above 5. But combination of all. As I have to know the return type, comparison between myclass() and myclass, and comparison between myclass() and Date()/predefined window objects.

Vineet 'DEVIN' Dev
  • 1,183
  • 1
  • 10
  • 35
  • 1
    Q1: Two ways of doing the same thing. like explained here http://stackoverflow.com/questions/3034941/new-myobject-vs-new-myobject – Sandeep Nayak Oct 14 '15 at 09:10
  • Q2: calling `new` only creates an instance of that type and does not call the method per se. So you can expect the result only when you do `console.log(myclass())` – Sandeep Nayak Oct 14 '15 at 09:20
  • Q3: [An interesting Article on the subject](http://www.bennadel.com/blog/2522-providing-a-return-value-in-a-javascript-constructor.htm) – aaroncarsonart Oct 14 '15 at 09:21
  • Q5: According to [this w3schools article](http://www.w3schools.com/jsref/jsref_tostring_date.asp), the Date object has a toString() method which returns a String representation of the Date. _This method is automatically called by JavaScript whenever a Date object needs to be displayed as a string._ – aaroncarsonart Oct 14 '15 at 09:23

1 Answers1

0

First of all, i will advice you to read Introduction_to_Object-Oriented_JavaScript.
You will find a lot of answers.

Quickly :

Q1- I dont see any difference between the behaviors of these two. Please explain the reason.

it is true in your use case, because your constructor don't need arguments.

Q2 - When I do - console.log(a), i expect to get "this is myclass" in console, but instead I see [object object].

this is because you try to log the Object not the result of a function.
you shoul do : console.log( a.m1() );

Q3 - If I return an object from the constructor of 'myclass'(eg - return {x:1}), then I cannot use 'm1' or 'mp1' anymore from the instances of 'myclass'.

simply : why would you do that ? returning something else than an instance for a constructor ?

Q4 - Comparing this with Date class of window object, this is totally different behavior. If a = new Date(), then i see value of 'a' as a date although the type is an object, and this points to my next question.

in reality there is an built-in function toString for every object and it is called when you try to "convert/to print" an object to string.


edit

from the comment of Ward D.S.
the default toString of anything simply returns the type. usually object Object. If you want this behaviour changed, simply overwrite the .prototype.toString of your class and return what you want


when you call : console.log(myDate);
the underground is console.log( myDate.toString() )
this is why you see your date string.

If you want the same behaviour you have to add :

var MyClass = function(){
  this.name = 'i am a MyClass';
}
MyClass.prototype.toString= function(){ return this.name };

var a = new MyClass();
document.write (a)

Q5 - The date class returns a date which looks like a string in DOM but the 'typeof' of the instance of date class is an object. Why's that?

because everything in javascript is an Object

Anonymous0day
  • 3,012
  • 1
  • 14
  • 16
  • Q2 and Q4 are very related. So if instance of Date class prints me a date as string then why instance of 'myclass' doesn't do that?? – Vineet 'DEVIN' Dev Oct 14 '15 at 09:27
  • @Vineet'DEVIN'Dev i have updated my answer to have the same beahvior – Anonymous0day Oct 14 '15 at 09:31
  • 1
    the default `toString` of anything simply returns the type. usually `object Object`. If you want this behaviour changed, simply overwrite the `.prototype.toString` of your class and return what you want. – Ward D.S. Oct 14 '15 at 09:32
  • Q2 - If instance of Date class prints me a date as string then why instance of 'myclass' doesn't do that?? Q3 - As you said in answer of Q4 that i would get typeof of the instance of 'myclass' always as object, then whats so wrong with returning {x:1}, What difference it makes when I return an object instead of a literal? Q4 - Ok. But why [Object Object] is shown for myclass, why not "this is myclass". Please compare it with Date class. Q5 -Why does the instance of Date class can be inserted in any DOM element as text? Please give attention to these qns, they are related – Vineet 'DEVIN' Dev Oct 14 '15 at 09:40
  • 2
    Anonymous0day answered those questions already? The class Date has overwritten its `toString` function to give a formatted date. When any object inserted in the DOM as text (or used as text anywhere else, like the console), `toString` is called (under the hood) to attempt to make the object into valid text. Override the function as shown in the answer to get the same behaviour. – Ward D.S. Oct 14 '15 at 09:46
  • Thanks you very much for your answers, specially for Q4. Really helped me over several confusions. – Vineet 'DEVIN' Dev Oct 14 '15 at 10:07
  • Please upvote the question if you think its going to help people learn javascript. --Thanks everyone – Vineet 'DEVIN' Dev Oct 14 '15 at 10:10