Live data from Hacker News

JavaScript: what is "this"?

howtonode.org

31–40 of 49 posts

Re: JavaScript: what is "this"?

#31
post #14

Earlier quoted context omitted.

Which is why PHP is so much less flexible than JS. Also try this: $a = 1; function qqq() { global $a; unset( $a ); } qqq(); var_dump( $a ); P.S.: In PHP 5.1.6 calling unset() actually increases memory usage...

I find it hard to believe that the largest cause (or even a significant cause) in the difference of flexibility of two languages is whether or not you can declare variables to belong to a given scope, and to which scope they default to.

I was referring more to the "feel" of the language rather than the fact that this individual feature is the single reason for why JavaScript is more flexible.

Re: JavaScript: what is "this"?

#32

Earlier quoted context omitted.

Which is why PHP is so much less flexible than JS. Also try this: $a = 1; function qqq() { global $a; unset( $a ); } qqq(); var_dump( $a ); P.S.: In PHP 5.1.6 calling unset() actually increases memory usage...

> Which is why PHP is so much less flexible than JS. When running, I want enough flexibility to move freely but not so much that my knees break. When programming, I'd like to rely on "undefined" not being 6, and I'd even like to have a reasonable built in dictionary type that other code can't extend to add keys to by default, or overwrite "hasOwnProperty".

I never claimed that JavaScript was better, just more flexible. I agree that all the things you mentioned are pain points with JS.

Re: JavaScript: what is "this"?

#33

Earlier quoted context omitted.

To clarify, you're saying super is difficult to implement in JavaScript? I think the power of JavaScript's design is borne out by how easily one can recreate classical inheritance in the language, a view shared by Crockford.

I'm not sure I understand. I'm saying specifically that at the very least, this one feature of classical inheritance is not easy to implement and in fact almost always leads to bugs when people try to do it. The truth is that most of the interesting features of classical inheritance are pretty hard to implement: 1. super, as described above 2. private, protected, etc. Never seen this work in js with inheritance 3. in…

> 1. super, as described above

Make the super property a reference to the parent prototype in your "extends" function. ExtJS does this with the "superclass" property.

> 2. private, protected, etc. Never seen this work in js with inheritance

What? This is basic stuff.

Private:

  var fn = function() {};
Protected:

  this.fn = function() {};
Public:

  Cls.prototype.fn = function() {};
> 3. inheriting class methods as well as instance methods

See above. May I refer you to http://www.crockford.com/javascript/inheritance.html ?

Re: JavaScript: what is "this"?

#36

Earlier quoted context omitted.

I'm not sure I understand. I'm saying specifically that at the very least, this one feature of classical inheritance is not easy to implement and in fact almost always leads to bugs when people try to do it. The truth is that most of the interesting features of classical inheritance are pretty hard to implement: 1. super, as described above 2. private, protected, etc. Never seen this work in js with inheritance 3. in…

> 1. super, as described above Make the super property a reference to the parent prototype in your "extends" function. ExtJS does this with the "superclass" property. > 2. private, protected, etc. Never seen this work in js with inheritance What? This is basic stuff. Private: var fn = function() {}; Protected: this.fn = function() {}; Public: Cls.prototype.fn = function() {}; > 3. inheriting class methods as well as…

1. As described this has serious issues when you reach depths greater than 1. This is why prototype has to use the hack of passing in a special super variable through the function arguments. This is also why in ExtJS, doing something that in any other language is a simple "super.method()" requires doing the following (from their own subclassing examples):

    Ext.ux.IconCombo = function(config) {
 
        // call parent constructor
        Ext.ux.IconCombo.superclass.constructor.call(this, config);
 
    };
Wow. Fantastic. Even if we ignore the Ext.ux, you can't tell me this isn't absolutely ridiculous. For starters, it requires access to the actual class object which is only slightly better than inlining the superclass' actual name yourself. It means if you ever change the class name there are additional points of failure (change it to IconComboBox, and now all your IconCombo.superclasses don't work if you happen to forget to modify them). Additionally, this is SO CONFUSING to beginners. Look, I can see that its "powerful" if thats what you want to call it, but people who want to write apps, and not sit around pontificating about languages have to understand so much to know what is happening in that one line of code. Beyond the verbosity, they need to get that they are taking someone else's function and then applying it to yourself while switching out the "this" (remember how I said this came into play with super calls). This is taking a problem that was hard to solve at the framework level and pushing it to the user.

2. For starters, you conveniently didn't mention how to handle private and protected member variables which is the actual interesting bit here. The goal is to not have people accidentally write into your internal data representations. This is modularization 101 and is really difficult to do with JS, since this._x = blah is accessible to EVERYONE. this._x is NOT protected in any way, every party has access to it and can change it. The "var fn" of course doesn't work with private member variables since you need one for each individual instance, you can of course do the closure trick inside your constructor:

function MyClass() { function() { var myVar = 5; this.getMyVar = function() { return myVar; } }

But this is recommended against by everyone since it 1. no longer has the benefit of modifying the prototype so its hard to inherit these methods without further trickery, and 2 is very very slow.

So no, this is not basic stuff. Not if you actually want to implement classical inheritance instead of just using names from classical inheritance and applying them to random orthogonal features.

3. Again, you run into the same super problems.

Look, I am willing to believe that people like prototypal inheritance, and I can respect the position that JS doesn't have classical inheritance because its not good or because you should use prototypal or whatever. But come on, lets not call a few hacks on top of it "classical inheritance".

Yes, I've seen Crockford's page. Its not particularly enlightening to this discussion. It presents several different forms of inherity-stuff, none of which are particularly classical in my opinion, which comes right back to the OP's original point: everyone ends up doing inheritance slightly differently and spends way too much time thinking about this problem. The fact that so much has been written on how to do inheritance in JS should be a red flag in and of itself. I will gladly take other language's "stricter less powerful" models because at least I can focus on programming and not the volumes and volumes that have been written on how to do something that should be as simple as inheriting methods.

Re: JavaScript: what is "this"?

#37

Earlier quoted context omitted.

> 1. super, as described above Make the super property a reference to the parent prototype in your "extends" function. ExtJS does this with the "superclass" property. > 2. private, protected, etc. Never seen this work in js with inheritance What? This is basic stuff. Private: var fn = function() {}; Protected: this.fn = function() {}; Public: Cls.prototype.fn = function() {}; > 3. inheriting class methods as well as…

1. As described this has serious issues when you reach depths greater than 1. This is why prototype has to use the hack of passing in a special super variable through the function arguments. This is also why in ExtJS, doing something that in any other language is a simple "super.method()" requires doing the following (from their own subclassing examples): Ext.ux.IconCombo = function(config) { // call parent construct…

> The "var fn" of course doesn't work with private member variables since you need one for each individual instance,

This makes me believe that you have no idea what you're talking about.

Re: JavaScript: what is "this"?

#38

Earlier quoted context omitted.

1. As described this has serious issues when you reach depths greater than 1. This is why prototype has to use the hack of passing in a special super variable through the function arguments. This is also why in ExtJS, doing something that in any other language is a simple "super.method()" requires doing the following (from their own subclassing examples): Ext.ux.IconCombo = function(config) { // call parent construct…

> The "var fn" of course doesn't work with private member variables since you need one for each individual instance, This makes me believe that you have no idea what you're talking about.

Normally I'd take the time to explain, but I'm pretty sure the code example right below that statement makes evident what I was getting at, and its pretty clear to me that you just aren't reading my responses, so there's really no point.

But you're probably right, I probably have no idea what I'm talking about despite having written a popular JS framework and the most widely used mobile browser on the planet.

Re: JavaScript: what is "this"?

#39
post #27

If anyone wonders why every JavaScript framework under the sun rolls its own object system, and they're all incompatible, and almost nobody uses native JavaScript alone anymore, this is one of the reasons.

The reason why some frameworks roll out custom object systems is because they try to make JavaScript look like a language with classes, "this" semantics isn't what's driving them. In a language with classes developers are used to "this" being an instance of the current class, and this expectation creates a consfusion in JavaScript. If you don't look at it through the prism of classes, the rule for "this" is pretty si…

You've left out event handlers, which are another case in which `this` can get confusing.

Re: JavaScript: what is "this"?

#40
post #6

Earlier quoted context omitted.

I think the author is just citing how influential Scheme was to JavaScript's design. In "Coders at Work", page 141, Brendan Eich talks about wanting to build a scheme-like language for the browser. The c-like syntax was adopted because Netscape wanted it to look like Java.

Maybe he wanted to. If that was his goal, I think he failed. Scheme: First-class functions, tail-call elimination, macros, hygienic macros, write and parse the AST directly. JavaScript: First-class functions. Scheme: Immutable arrays and lists, mutable strings. JavaScript: Mutable-only arrays, immutable-only strings. Yes, I have, and have read, Coders at Work. I don't see how it's relevant. JavaScript and Scheme shar…

You have to compare JavaScript with Scheme of the time when JavaScript was designed. Both macros and immutable structures are pretty recent (1999 and 2006 IIRC) additions to Scheme, previously such facilities were either non-existent or implementation-specific.

While Scheme's syntax superficially looks like it's data, specification does not imply that there should be accessible AST as list structure anywhere (in contrast to Common Lisp, which is specified in terms of list structures and algorithm to build them from text serialization)

And by the way: You have left off the one most important defining feature of Scheme: first-class continuations. And JavaScript really does not have them (I would say that this is completely reasonable omission for JS's purpose)

Post reply on HN