getter/setter in prototype

360 views
Skip to first unread message

Suller Andras

unread,
Oct 20, 2011, 8:30:44 AM10/20/11
to ang...@googlegroups.com
Hi guys,

Today we ran into an unusual thing: if a getter defined in the
prototype, it is not working in angular. Is there anything special
about this kind of getters?
Here is a jsfiddle about it: http://jsfiddle.net/UrXxq/
It would be nice if we can define getters and setters in the prototype
with the other functions.

Thanks,
Andras

Robert B. Weeks

unread,
Oct 20, 2011, 11:15:22 AM10/20/11
to ang...@googlegroups.com
Hey Suller -

If I changed the call in your html to:

getter defined in the prototype: {{value2()}}

and the prototype:

ApplicationCtrl.prototype = {
value2: function() {
console.log('value2 called!')
return this._value
}
}

works OK. The prototype you had wasn't defining an actual method.

HTH.

Suller Andras

unread,
Oct 20, 2011, 12:14:30 PM10/20/11
to ang...@googlegroups.com
Hey Robert,

The prototype I showed should define a getter method. It is working
fine in nodejs, and I'd like to know if there is any reason why they
are not working with angular.
Or maybe it is a but that it is working in node?

Andras

> --
> You received this message because you are subscribed to the Google Groups "AngularJS" group.
> To post to this group, send email to ang...@googlegroups.com.
> To unsubscribe from this group, send email to angular+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/angular?hl=en.
>
>

Elliott Sprehn

unread,
Oct 20, 2011, 12:22:46 PM10/20/11
to ang...@googlegroups.com
This is a side effect of some black magic that angular does to create
scopes and attach controllers.

Scopes aren't really instances of the controller, it's an instance of
a class inside of angular with the controller mixed into it.

https://github.com/angular/angular.js/blob/master/src/Scope.js#L181

Misko Hevery

unread,
Oct 20, 2011, 1:39:52 PM10/20/11
to ang...@googlegroups.com
a slightly better explanation is that when angular creates a new scope it copies the properties from the prototype into the instance, but the property in this case acts just like a regular property not as a function, hence it does not get copied properly.

This will be solved soon since we are going to start using real instances of controllers with the scope injected into them.

-- Misko

Reply all
Reply to author
Forward
0 new messages