Richard Lynch wrote:
On Tue, April 26, 2005 7:51 pm, Eli said:

I know this is not the forum, but I googled and couldn't find it, so
please try to help me with this.

/*********/
function MyCls(name)
{
   this.name=name;
}
function SayHi()
{
   alert('Hi, '+this.name+'!');
}
var obj=new MyCls('PHP');
obj.name='JavaScript';  //this will call SayHi() function
/*********/

I have a class in JS with a property variable in it. How can I execute a
function when the property value is changed?


I don't think JavaScript has any way to hook into a property being changed
and take some action on that...

It *MIGHT* have 'private' properties, that can't be changed by child
classes, and then you'd have to use a function to change the property, and
then your function that changes the property can do whatever it wants...

But that all assumes JavaScript even has "private" properties, which it
may not have.

..it has closures - which allow you to create/emulate private properties as you know them from other langs, I abuse this occasionally but I hardly understand it. (so what Richard described can be done in JS)

a couple of points about JavaScript:
1. everything is a object!
2. the object model is prototype based, which is quite orthoganol to PHP's 
object
model (for instance) - something that is liable to bite you in the ass a few 
times.
3. is a _lot_ more powerful (and well thought out) than its given credit for.
4. in terms of being a 'dynamic' language it makes PHP look like its statically 
typed
& compiled ;-) - which is in no way a dig at PHP (which is lovely just the way 
it is :-)

nice one Marek for the heads up on the watch() method, much obliged to you!
here is a link that explains the watch method for those too lazy to google:
http://www.devguru.com/Technologies/ecmascript/quickref/watch.html

now lets get back to PHP on this list shall we ;-)


Your only other option is to just DOCUMENT that nobody should ever alter ".name" directly, but should always use your changeName() function which does whatever you want it to do.


-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to