Re: 'this' confusion (heading off topic)



Thomas 'PointedEars' Lahn wrote:
On the contrary, what `this' would refer to is quite easy to determine if
you imagine the context in which it occurs to be entered. Because, as in
other programming languages supporting this mechanism, it is the caller that
determines the `this' value. And in ECMAScript implementations, where there
is no caller (or the caller is the Global Object), `this' refers to the
Global Object.

A good thing to bear in mind while programming Javascript.

Even with the explanation, it is a little disturbing to the uninitiated
as you call us that these are all different:
setInterval(obj.func, 1000)
setInterval("obj.func", 1000)
setInterval("obj.func()", 1000)
setInterval(obj.func(), 1000)

I think most of why it makes sense in Javascript for me, and it is just
my opinion, is the consistency it gives to the idea of the function as
an object assigned as a value of a property.

In a language with classes, it may be reasonable for function objects to
be tied to the object they are defined on. Not saying it has to be one
way or the other, though, just that there is a rationale in that the
function can be considered more closely linked to the object because of
the class definition.

Since you brought up the comparison of languages, I thought I'd throw in
a possible counter-example. I feel the language Ruby, in at least one
context, acts as I suggested. One can grab methods from objects using
Object.method, which remain associated with the originating object:

"Method objects are created by Object#method, and are associated with a
particular object (not just with a class). They may be used to invoke
the method within the object, and as a block associated with an
iterator." -- Programming Ruby (Hunt and Thomas)

On the other hand, there are proc objects which are not directly bound
to an originating object (but to closures...).

Jon.
.



Relevant Pages

  • Re: How Do CLs Macros Differ from Schemes?
    ... context-sensitive grammars are of little help in programming ... languages and are not widely used at all. ... What is a context-sensitive grammar? ... In a context sensitive grammar we might have stuff like. ...
    (comp.lang.lisp)
  • Re: Unix and OO Avoidance
    ... I feel the need to restore the context because in quite a few places, ... Whether functional methods and languages limit the negative impact of ... supporting structured programming, in your linguistic framework, we must ...
    (comp.object)
  • Re: About context sensitivity.
    ... I heard of Anders Heljsberg eplaining the idea of Data Programming. ... This means semantic net based Semantic ... Level context programming language available? ... > sensitivity and if some programming languages exist that target .NET. ...
    (microsoft.public.dotnet.framework.clr)
  • Re: About context sensitivity.
    ... languages can have classes that respond differently to different situations. ... VB.NET is very much about event-driven programming. ... > semantics I mean a function of class reads some sort of Meta Data and ... in the context of that. ...
    (microsoft.public.dotnet.framework.clr)
  • Re: compiler for Chinese development language
    ... This relates to the development of vernacular ... Indian vernacular display, OS and programming languages. ... Bangla and other vernaculars. ...
    (comp.compilers)