LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-08-2006, 07:39 AM   #1
tumana
Member
 
Registered: Mar 2004
Location: Belgium
Posts: 102

Rep: Reputation: 15
JavaScript: Changing a class property


I was learning about OOP in JavaScript and I wrote this little script that defines an object, Robot, with a class property, a prototype property, and one prototype function. And a constructor, of course.

Code:
<script type="text/javascript">
<!--

// Class property
Robot.threeLawsSafe =  true;

// Prototype property
Robot.prototype.model = "BR-111";

// Prototype function
Robot.prototype.info = function() {
  with(document) {
    write("Name: " + this.name + "<br />");
    if(this.threeLawsSafe) {
      write("This robot is safe for the whole family.<br /><br />");
    }
    else {
      write("This robot might kill you and your whole family.<br /><br />");
    }
  }
}

// Robot Constructor
function Robot() {

}

// First Robot
var iRobot  = new Robot();
iRobot.name = "James";
iRobot.info();
// Robot.threeLawsSafe should still be set to true... but it's not. ??

// Second Robot
var evilRobot = new Robot();
evilRobot.name = "Matador";
// Robot.threeLawsSafe is now set to false
Robot.threeLawsSafe = false;
evilRobot.info();

// Print the status of the first Robot
iRobot.info();
// Robot.threeLawsSafe is understandably false now.

//-->
</script>
I expected the output to be the following
Code:
Name: James
This robot is safe for the whole family.

Name: Matador
This robot might kill you and your whole family.

Name: James
This robot might kill you and your whole family.
My reasoning was
1. Define object
2. Create first Robot, James, and print out his information.
3. Create a second robot, Matador, and override the class property Robot.threeLawsSafe to false.
4. Print out its information
5. Print out the 1st robot's info and the class property would be changed as well.

Instead, I got this
Code:
Name: James
This robot might kill you and your whole family.

Name: Matador
This robot might kill you and your whole family.

Name: James
This robot might kill you and your whole family.
It seems that before it even printed out the first object's info the second object already changed the class property Robot.threeLawsSafe to false.

Why? How do I make the script wait till the first object prints out his stuff before changing threeLawsSafe?

Thanks in advance.

<edit>
I just changed something [ever so tiny] in the script to make it more legible. It's the same script though.
</edit>

Last edited by tumana; 02-09-2006 at 02:29 AM.
 
Old 02-14-2006, 07:18 AM   #2
german
Member
 
Registered: Jul 2003
Location: Toronto, Canada
Distribution: Debian etch, Gentoo
Posts: 312

Rep: Reputation: 30
You're mistaking the scope of the variable "this". This code does what you want:

Code:
<script type="text/javascript">
<!--

// Class property
Robot.threeLawsSafe =  true;

// Prototype property
Robot.prototype.model = "BR-111";

// Prototype function
Robot.prototype.info = function() {
  with(document) {
    write("Name: " + this.name + "<br />");
    if(Robot.threeLawsSafe) {
      write("This robot is safe for the whole family.<br /><br />");
    }
    else {
      write("This robot might kill you and your whole family.<br /><br />");
    }
  }
}

// Robot Constructor
function Robot() {

}

// First Robot
var iRobot  = new Robot();
iRobot.name = "James";
iRobot.info();
// Robot.threeLawsSafe should still be set to true... but it's not. ??

// Second Robot
var evilRobot = new Robot();
evilRobot.name = "Matador";
// Robot.threeLawsSafe is now set to false
Robot.threeLawsSafe = false;
evilRobot.info();

// Print the status of the first Robot
iRobot.info();
// Robot.threeLawsSafe is understandably false now.

//-->
</script>
 
Old 02-15-2006, 01:35 AM   #3
tumana
Member
 
Registered: Mar 2004
Location: Belgium
Posts: 102

Original Poster
Rep: Reputation: 15
Thumbs up

Wow! That makes sense. To get the value of PI, I wouldn't write
PHP Code:
var this.PI
I would write
PHP Code:
var Math.PI
Thanks!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem after changing IP class nazir Linux - Networking 4 01-22-2006 06:25 PM
javascript: changing to a given style defined in a ccs eantoranz Programming 2 11-17-2004 02:26 PM
changing an HTML table with javascript eantoranz Programming 2 10-29-2004 12:21 PM
changing priority class from the command line stellarmarine1 Linux - General 5 08-21-2004 08:32 PM
BlackBox.class & VerifierBug.class virus ??? dalek Linux - Security 4 02-29-2004 08:55 AM


All times are GMT -5. The time now is 10:16 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration