This() vs. Holder()

949 views
Skip to first unread message

ondras

unread,
Dec 6, 2010, 9:37:37 AM12/6/10
to v8-users
Hi,

one more question to V8 gurus: what *exactly* is the difference
between This and Holder (in v8::Arguments and v8::AccessorInfo)? I
just encountered [1] and realized that I use args.This() in many
places in my code - mostly to store/retrieve internal fields...


Thanks,
Ondrej

[1] http://groups.google.com/group/v8-users/browse_thread/thread/07aa26a3feaf605a/e05a83108c71b99e#e05a83108c71b99e

Stephan Beal

unread,
Dec 6, 2010, 12:50:42 PM12/6/10
to v8-u...@googlegroups.com
On Mon, Dec 6, 2010 at 3:37 PM, ondras <ondre...@gmail.com> wrote:
one more question to V8 gurus: what *exactly* is the difference
between This and Holder (in v8::Arguments and v8::AccessorInfo)? I
just encountered [1] and realized that I use args.This() in many
places in my code - mostly to store/retrieve internal fields...

--
----- stephan beal
http://wanderinghorse.net/home/stephan/

Ondřej Žára

unread,
Dec 6, 2010, 2:41:11 PM12/6/10
to v8-u...@googlegroups.com
Hi Stephan,

thanks for this link. After reading the thread, I conclude that This
and Holder can be safely treated as the same, as long as I can
guarantee that my method is called within the correct object (e.g. the
method is not assigned to a different object's property).


Thanks,
Ondrej

2010/12/6 Stephan Beal <sgb...@googlemail.com>:

> --
> v8-users mailing list
> v8-u...@googlegroups.com
> http://groups.google.com/group/v8-users

Stephan Beal

unread,
Dec 6, 2010, 3:02:04 PM12/6/10
to v8-u...@googlegroups.com
On Mon, Dec 6, 2010 at 8:41 PM, Ondřej Žára <ondre...@gmail.com> wrote:
thanks for this link. After reading the thread, I conclude that This
and Holder can be safely treated as the same, as long as I can
guarantee that my method is called within the correct object (e.g. the
method is not assigned to a different object's property).

That's how i interpret it as well. If we're wrong, i hope one of the v8 gurus who is reading this will correct us :). 

Anton Muhin

unread,
Dec 7, 2010, 11:44:31 AM12/7/10
to v8-u...@googlegroups.com
Ondřej, Stephan,

On Mon, Dec 6, 2010 at 11:02 PM, Stephan Beal <sgb...@googlemail.com> wrote:
> On Mon, Dec 6, 2010 at 8:41 PM, Ondřej Žára <ondre...@gmail.com> wrote:
>>
>> thanks for this link. After reading the thread, I conclude that This
>> and Holder can be safely treated as the same, as long as I can
>> guarantee that my method is called within the correct object (e.g. the
>> method is not assigned to a different object's property).
>
> That's how i interpret it as well. If we're wrong, i hope one of the v8
> gurus who is reading this will correct us :).

They are not the same. This() corresponds to JS 'this' and should be
operated upon when you'd like to have a normal JS semantics.

Holder() is the objects which is instance of your FunctionTemplate.

That's not always easy to see the difference, but Christian gives a
nice example: x = { __proto__: document }, x.createElements("div").
In this case This would point to the objects pointed by 'x' variable
as well and Holder would point to object pointed by 'document'.
Therefore, if you expect some internal structure (for example, many
host objects wrap native objects and store pointers to them into
internal fields), attempt to use This would fail (it should have no
internal fields, or worse, you'll get wrong pointer), but Holder
should have expected internal fields.

Overall Holder should be always in the prototype chain of This and
hence if you read the property, you can freely use both. However
setting the property would behave differently if This() != Holder()
--- the property would end at different object.

So in each context This vs. Holder should be carefully chosen.

yours,
anton.

Stephan Beal

unread,
Dec 7, 2010, 12:11:40 PM12/7/10
to v8-u...@googlegroups.com
On Tue, Dec 7, 2010 at 5:44 PM, Anton Muhin <ant...@chromium.org> wrote:
...So in each context This vs. Holder should be carefully chosen.

Thank you very much for the clarification, Anton.
Reply all
Reply to author
Forward
0 new messages