Visit Mozilla.org

Talk:Core JavaScript 1.5 Reference:Operators:Member Operators

From MDC

First, some of the stuff from Core_JavaScript_1.5_Guide:Objects_and_Properties should be migrated here.

Second, I don't know what these operators are called exactly. ECMAScript calls both "property accessors", but the guide here calls them "member operators". ECMAScript calls the dot operator "dot notation" and the subscript operator "backet notation".

--Maian 20:58, 25 Aug 2005 (PDT)

The stuff from devedge - JS reference and guide - does use different terminology from ECMA spec. I think we should use names consistent with the guide, but note that it's called different in ECMA spec. --Nickolay 01:09, 26 Aug 2005 (PDT)

Another thing I should mention is that I don't know the history of these properties. The dot notation obviously has always been in JS, but I'm not sure about the bracket notation.

--Maian 03:41, 31 Aug 2005 (PDT)

I don't think we care much about history. --Nickolay 05:21, 31 Aug 2005 (PDT)

[edit] method binding

A mistake that some novices make is to do something like this:

var dln = document.writeln;
dln('foo');

which works in IE but not on Mozilla (at least on the version I'm using). Unless the function is bound to the document object, this shouldn't work. Another example, this time, DOM-independent (except for the alert call):

function A(x) = { this.x = x; };
A.prototype.foo = function() { alert(this.x); };

var bar = new A(10);
var foo = bar.foo;
foo();  //alerts undefined, since |this| now refers to the global object

Unlike Python, methods are not bound to their parent objects in JS. Instead, the member operator supplies the this object in a method call, e.g. continuing the above example, the following are equivalent:

bar.foo();
foo.call(bar);

If neither the member operator nor call() are used, which is the case for foo(), this then refers to the global object.

So this leads to 2 questions:

  1. Is this too esoteric? Should this even be mentioned anywhere here?
  2. This reference is missing a function call section, so should that be added? Should this magical this-supplying effect of the member operator be discussed there?

--Maian 02:39, 26 Aug 2005 (PDT)

This is quite important topic, and I know people get confused about this (e.g. addEventListener("load", myext.onLoad, false)). So if this isn't discussed in the reference, please add it. --Nickolay 12:27, 26 Aug 2005 (PDT)
I'm not sure if the section on "this being supplied by function calls" belongs here. This whole method binding section is getting big for a page describing only member operators. --Maian 04:11, 5 September 2005 (PDT)