JavaScript is a very strange place for me...
So, I decided that, before I attempt to create an Object, I should first learn about Objects. I had actually created one before with the help of a tutorial. The tutorial was complicated because showed me how to create a JavaScript Object that is a representation my .NET Object and how to get the two to "work together as one".... apparently the tutorial was not very clear about what was going on in the JavaScript Object...and even with my extensive commenting of the JavaScript Object I still didn't "really" know what was going on...at the time I was more interested in the .NET concepts than the JavaScript techniques being used in the tutorial.
Anyways, I started with the basics: how do I create a JavaScript Object.
To my surprise all functions were Objects, all Objects were Functions and I was not pleased with this new way of thinking because it's really strange...but it's just how things are.
The next thing I did was try to figure out what I had done to create the JavaScript Object in the tutorial. This Object uses the prototype property (which is apparently an Object and is a property of all functions) to add functions and properties to the Object.
So I started playing around with a silly little (uncomplicated) Object of my own...
Please see the comments in the following code for what I'm confused about:
Thanks for your help clarifying the questions in the above posted code!
-Frinny
So, I decided that, before I attempt to create an Object, I should first learn about Objects. I had actually created one before with the help of a tutorial. The tutorial was complicated because showed me how to create a JavaScript Object that is a representation my .NET Object and how to get the two to "work together as one".... apparently the tutorial was not very clear about what was going on in the JavaScript Object...and even with my extensive commenting of the JavaScript Object I still didn't "really" know what was going on...at the time I was more interested in the .NET concepts than the JavaScript techniques being used in the tutorial.
Anyways, I started with the basics: how do I create a JavaScript Object.
To my surprise all functions were Objects, all Objects were Functions and I was not pleased with this new way of thinking because it's really strange...but it's just how things are.
The next thing I did was try to figure out what I had done to create the JavaScript Object in the tutorial. This Object uses the prototype property (which is apparently an Object and is a property of all functions) to add functions and properties to the Object.
So I started playing around with a silly little (uncomplicated) Object of my own...
Please see the comments in the following code for what I'm confused about:
Code:
<html>
<body>
<script type="text/javascript">
/*Welcome to my little Foo Class experimentation.
The following function is a "constructor function",
that will create an instance of my Foo Object*/
function Foo(firstNumber,secondNumber){
this.a = firstNumber; //"a" is now a property of the Foo Object
this.b = secondNumber;//"b" is now a property of the Foo Object
this.Multiply = function(){return this.a * this.b;}//Multiply is now a method for my Foo Object
this.Divide = function(){return "you're in Foo's Divide function";}
//The Divide method is NOT overwritten by the Divide method in the prototype Object
/*
The following does not work.
Even though it does work when I use the same sort of
thing to "overwrite" the prototype Object.
Why??
*/
/*a: firstNumber,
b: secondNumber,
Multiply: function(){return this.a * this.b;}*/
};
//This was my first experiment:
//I used the prototype property to add the Divide method to the Foo Class
/*Foo.prototype.Divide = function(){return this.a/this.b;};*/
//This is my second experiment:
//overwrite the prototype Object
//This works fine but...
//What is with the colons (:) and commas (,)??
//I don't get it
Foo.prototype = {
Add: function(){return this.a + this.b},
Divide: function(){return this.a/this.b;}
};
var instanceOfFoo = new Foo(2,3);
//JavaScript couldn't find Add in the Foo class
//it looks to the prototype Object and finds it there
document.write(instanceOfFoo.Add());
document.write("<br />");
document.write(instanceOfFoo.Multiply());
document.write("<br />");
//The Divide method is found in the Foo class and is used
//instead of the one defined in the prototype
document.write(instanceOfFoo.Divide());
document.write("<br />");
//The following doesn't work...
//how do you access the Divide method in the prototype???
//document.write(instanceOfFoo.prototype.Divide());
//document.write("<br />");
document.write("<br />");
var anotherInstanceOfFoo = new Foo(10,2);
document.write(anotherInstanceOfFoo.Add());
document.write("<br />");
document.write(anotherInstanceOfFoo.Multiply());
document.write("<br />");
document.write(anotherInstanceOfFoo.Divide());
document.write("<br />");
document.write("<br />");
instanceOfFoo.newProperty = "instanceOfFoo's property";//adds a property named "newProperty" to the instanceOfFoo live Object.
document.write(instanceOfFoo.newProperty);
document.write("<br />");
document.write(anotherInstanceOfFoo.newProperty);//is undefined because it is only available to the instanceOfFoo live Object
document.write("<br />");
document.write("<br />");
//Adding a property named "newProperty" to the prototype object of the Foo class
//This makes it available to all instances of the Foo class (live ones too)
Foo.prototype.newProperty ="newly added property to prototype which is available to all
instances of Foo.";
//It's interesting that the original value for the "newProperty" property is kept
document.write(instanceOfFoo.newProperty);
document.write("<br />");
document.write(anotherInstanceOfFoo.newProperty);//now contains a value
document.write("<br />");
</script>
</body>
</html>
Thanks for your help clarifying the questions in the above posted code!
-Frinny
Comment