Many people know that there are so-called getters and setters in Javascript. These are constructs for tracking changes in object property values, as well as returning these values. "From the inside" of the object, they look like normal functions:
var obj = {
get value() {
return 0;
},
set value(s) {
alert ( "Go screw yourself!" )
}
}
* This source code was highlighted with Source Code Highlighter .
But since these are functions, they can be used as you please!
')
For example, we can make a constructor that will simplify our work with CSS of a certain HTML element:
function $( object ) {
if ( typeof ( object )== "string" ){ object = document .getElementById( object )};
object .__defineGetter__( "x" , function (){
return (parseInt( object .style.left))
}),
object .__defineSetter__( "x" , function (n){
object .style.left = n + 'px'
}),
object .__defineGetter__( "y" , function (){
return (parseInt( object .style.left))
}),
object .__defineSetter__( "y" , function (n){
object .style.top = n + 'px'
})
}
* This source code was highlighted with Source Code Highlighter .
Now the button with the id "btn1" can be moved by writing
$("btn1").x = 56
.
But the beauty of Getters and Setters is not only in simplifying everyday tasks, such as working with CSS. They can even be used to change the concept of the language itself:
For example, the following example displays a message:
obj = {
set message(s) {
alert(s)
}
}
obj.message = "Hello, World!"
* This source code was highlighted with Source Code Highlighter .
Fearfully? In my opinion, very much. The main thing in this business is not to reach the point of absurdity. There is a mathematical application for this:
For example, we have an Obj object that has a value property, as well as a sin property. Turning to the sin property, we get the sine of value. The point is that with Setter you can do the opposite. Then after changing sin, value will change. In short, if sin == 0.5, then value = Math.PI / 6.
obj = {
value: 0,
get sin() {
return Math.sin( this .value)
}
set sin(n) {
this .value = Math.asin(n)
}
}
* This source code was highlighted with Source Code Highlighter .
It is worth noting that the decision is not quite cross-browser. If you want to port it all to your favorite
Internet Explorer browser, you should use __defineGetter __ () and __defineSetter __ (), or the methods described
here , and, in extreme cases,
here .
But the possibilities of Getters and Setters are really many. If you combine everything with the same AJAX, you can achieve quite a kosher look like:
ajax.url = "script.php"
alert(ajax.result)
* This source code was highlighted with Source Code Highlighter .
This, of course, will confuse some programmers who are used to the fact that the assignment operation does nothing but assignment (actually, like the value-taking operation). But one thing, you have to do something with methods without parameters, such as .Destroy (), because Obj.Destroy = true will definitely not be written. There is an idea to do something like a tick timeout, i.e. track each get and set. This means that Obj.Destroy = 5 will destroy the object after 5 get / set operations. But this, of course, is already too much creativity.