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"> Code:
Name: James 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 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> |
You're mistaking the scope of the variable "this". This code does what you want:
Code:
<script type="text/javascript"> |
Wow! That makes sense. To get the value of PI, I wouldn't write
PHP Code:
PHP Code:
|
All times are GMT -5. The time now is 01:56 PM. |