Live data from Hacker News

JavaScript: what is "this"?

howtonode.org

11–20 of 49 posts

Re: JavaScript: what is "this"?

#11
post #8
post #3

It's not entirely our fault, the language was designed to work like one thing (scheme-like), but look like another (c-like). The baseless assertion rears its head again. People keep repeating this, and it's not true. JavaScript is not Scheme. It's not any more Scheme-like than Java, C++, Python, Perl, or many other languages. Lua and Ruby are far more Scheme-like than JavaScript. I've commented before on people repea…

This point of view is also maintained by D Crockford[1] who is supposed to know JavaScript's background. [1] IIRC eg in this presentation: http://www.infoq.com/presentations/The-State-and-Future-of-J...

"Some other guy who other people say is right said something similar. Here is a link to a talk he once did."

Re: JavaScript: what is "this"?

#12
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.

Re: JavaScript: what is "this"?

#13
post #3

It's not entirely our fault, the language was designed to work like one thing (scheme-like), but look like another (c-like). The baseless assertion rears its head again. People keep repeating this, and it's not true. JavaScript is not Scheme. It's not any more Scheme-like than Java, C++, Python, Perl, or many other languages. Lua and Ruby are far more Scheme-like than JavaScript. I've commented before on people repea…

Egh, some of those other languages are more Scheme-like than others. In particular, Javascript is certainly more scheme-like than Java, which lacks even first-class functions, for example. And, well, Python is basically just Lisp with different syntax (or so Norvig claims; I'm not sure I'd go that far: http://norvig.com/python-lisp.html).

So I can't really agree that it's not any more Scheme-like than anything else out there. I do agree it's not particularly Scheme-like.

Re: JavaScript: what is "this"?

#14
post #9

Honestly, I think this is all too complicated. In Javascript, when you look at a line like this: a=1 you have to guess in which scope it changes or creates the variable a. I prefer the way PHP is dealing with scope. You simply know that "a=1" is only affecting the current scope.

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.

Re: JavaScript: what is "this"?

#15

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.

Would you expand on this? Most frameworks share some convenience functions that make working in js or DOM easier and put all of their functions within a particular namespace instead of spamming global. Whats the problem?

Re: JavaScript: what is "this"?

#16
This kind of a bind is available in dojo via dojo.hitch and in google closure via goog.bind (make sure to require goog.base)

There's a $.hitch plugin in JQuery, it doesn't come with it by default.

Re: JavaScript: what is "this"?

#17
post #9

Honestly, I think this is all too complicated. In Javascript, when you look at a line like this: a=1 you have to guess in which scope it changes or creates the variable a. I prefer the way PHP is dealing with scope. You simply know that "a=1" is only affecting the current scope.

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...

global $a in PHP is just another way to write $a =& $GLOBALS['a'];

I.e. you just create reference to a global variable. Unsetting reference just breaks the tie between variable name and content.

Re: JavaScript: what is "this"?

#19

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.

> rolls its own object system

=== "Provides convenience functions for working with prototype chains." That has nothing to do with what makes the this keyword special in JavaScript, beyond prototypes affecting this' properties.

Re: JavaScript: what is "this"?

#20
post #9

Honestly, I think this is all too complicated. In Javascript, when you look at a line like this: a=1 you have to guess in which scope it changes or creates the variable a. I prefer the way PHP is dealing with scope. You simply know that "a=1" is only affecting the current scope.

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".

Post reply on HN