Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Understanding OO Design 2

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
I was reading thread thread216-1282657 and think I had that flash of light moment and would appreciate your input!

in the thread it shows this as a simple example
Code:
function obj(){
  this.myproperty = 1;
  this.mymethod = function(){
    alert("Hi");
  }
}
var myObj = new obj();[code]
now does this mean 'obj' function is a class - defining the components / vars / methods which make up the object of class 'obj'

then myObj = new obj(); creates a new object of class 'obj'.

which has a method 'mymethod' and a property 'myproperty' which is of value '1'.

So if I want to perform the Method on the object 'myObj' of class 'obj' I use...
Code:
myObj.mymethod();
If I want change a property value i use
Code:
myObj.myproperty = 20;

To expand on this to create other properties and methods i would do
Code:
function mySum(){
this.mynum1 = 0;
this.mynum2 = 0;
this.total = 0;
this.sumit = function(){
    this.total = this.mynum1 + this.mynum2;
    alert("Sum Total : " + this.total;
  }
}
var mySumObj = new mySum();

function showSum(){

var frm = document.getElementById('myformid');

if(parseInt(frm.myfield1.value) > 0 && parseInt(frm.myfield2.value > 0){
  mySumObj.mynum1 = frm.myfield1.value;
  mySumObj.mynum2 = frm.myfield2.value;
  mySumObj.sumit();
}
else{alert("Input invalid");}
}

I have a form with two input fields and a button with an onclick which calls showSum.

Have I grasped this OO thing finaly?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Have I grasped this OO thing finaly?

Yup, pretty much. [thumbsup2]

-kaht

Looking for a puppy?

silky-icon-left.gif
[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
silky-icon-right.gif
 
I agree that you grasp Javascript's OO ideas pretty well.

Functions are objects, and can be used like C++/Java classes or methods. Variables are also objects, and can have the 2 previously-mentioned objects assigned to them as well, as your example shows. Everything is an object in Javascript, and determining the type of object is done when the script is executed.

Lee
 
WOOHOO - man that was a headache, but you know when you have that lightbulb moment, better go back to the other thread and give the man a star!

thanks kaht.



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Once you've got a firm grasp on creating objects, go look at using the prototype method to assign methods to specific object types.

One of the more popular ones that gets used in this forum is the trim method that is "assigned" to the string object using the prototype method:
[small](note the sexy regexp used to cut this down to 1 line!)[/small]
Code:
<script type="text/javascript">

String.prototype.trim = function() {return this.replace(/^\s+|\s+$/g, "");};

var a = "         blahblahblah             ";
alert(a.trim());

</script>

You can even use prototype to attach custom methods to user-defined objects.

(don't confuse this prototype with the prototype framework)

-kaht

Looking for a puppy?

silky-icon-left.gif
[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
silky-icon-right.gif
 
(don't confuse this prototype with the prototype framework)
i'm glad you said that coz that's exactly what i was thinking. so now i'm confused again - thanks kaht!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
prototype is a built-in javascript method that binds functions to a specific data-type, allowing custom methods for a specific data type. In the example I posted above it bound the trim method to the String data type, so in the rest of your application any time you have a string variable you could call the .trim() method to remove the leading and trailing spaces from the string. I thought that the creators of the prototype framework chose a poor name to call it since a prototype method already existed in the language they were targeting....

-kaht

Looking for a puppy?

silky-icon-left.gif
[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
silky-icon-right.gif
 
I thought that the creators of the prototype framework chose a poor name to call it since a prototype method already existed in the language they were targeting....
you're telling me!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top