So I know javascript pretty well, but I'm not sure about this one.
Tough to explain so I'll just show it:
var view = new View();
view.rating = 4.5;
Is there anyway to have view.rating be called as a function to manipulate the DOM a bit and set that rating to five?
So in View:
View.prototype = {
rating : function() {
$('div.stars').width(4.5);
}
}
I know there are workarounds, using something other than "view.rating = 5". I'm not interested in those. I'm wondering if there is a way to set this up maintaining view.rating = 5. This is coming JS is coming straight from PHP essentially and I don't want to bog the PHP down with anonymous functions.. like view.rating = function() {...};
Thanks! Matt Mueller
No. ( Edit : not on all implementations of common browsers at least)
There isn't anything in Javascript equivalent to "overloading the =
operator".
Edit :
I'm not sure if this is clear to you. If you make rating
a function (either via the prototype or not), you should call it like view.rating(5)
, and it can update the DOM (or do anything).
Most implementations of JavaScript don't support creating getter/setter properties so it wouldn't be super useful on the web. However there are some implementations that do. Here's an example in Mozilla's implementation:
EDIT: Let me just clarify, writing getters/setters is syntactic sugar over the use of a getFoo()/setFoo(foo) style property. It is for the developer's benefit. IMO, for a web app (due primarily to IE's lack of support) it isn't quite realistic just yet. While IE market share is dropping, depending upon your demographics in a widely public app, a lot of your visitors at this point still won't have support for them.
Some more links of interest for you:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.