I disagree about call and apply though. I prefer to never use:
new
this (one exception)
Object.create
bind
call
apply
When I avoid this list my code tends to be smaller and faster to execute. You don't need OOP in this language, thanks to lexical scope, to achieve decomposable reuse and reference sharing.
The one exception for this is event handlers where it is commonly necessary to access the event target without access to an explicit reference.
It might not be a requirement to use call or apply, but I think as a developer it is important to know about them and how they work though. Until ES6 is better supported, we're stuck with call, apply and bind. Javascript is a complex beast, I think it is important to know about many facets even if you don't use them. Spread operators are the future.
Until ES6 is better supported, we're stuck with call, apply and bind.
I don't see why ES6 is a factor in that discussion. I have seen several posts comments in this sub-reddit that make it sound like developers need ES6 right now just to write code, as though they cannot possibly write JavaScript without something like Babel.js.
I am curious where this line of thinking comes from. Maybe those people are all incredibly new to JavaScript or maybe they have never learned to build large applications in this language without it looking like Java. I would like to learn more about why people are forming such conclusions.
Fat arrow functions lexically bind this value, so it is more predictable for people coming from other languages.
When you have window as a this value it doesn't help anyone, it's clearly not intended, and depending in the caller of the function to call it correctly is risky
Is it really? You have a click event on a button. If you are able to click that button a thousand times a second, I might see a reason to care about a few clock ticks of performance, but for the use case bind provides an easy readable way to implement event binding.
Bind and apply are not likely to significantly impact performance enough to measure. It is not for performance reasons that I avoid these. I don't use them is because they are unnecessary.
Coming from a C++ like language you absolutely need this stuff. You need classes and a way to extend those classes or reference an object property in relation to a known context. Each of these features requires code definitions (overhead). A code author has to manually write code to define points of inheritance and bindings that connect various objects together and establish the organization of the code.
C++ has been around since 1982 and did not get lexical scope until 2008 (i think). Java did not get lexical scope until April 2014 (hasn't even been a year). A functional approach to writing code is available to these languages now, but it has not always been that way. Developer who grew up in those language only know the OO way that requires lots of code overhead. The OO way is not absolutely necessary any more in these languages, but this line of thinking is institutionalized.
JavaScript had lexical scope in 1995 when it was born. The advantage to lexical scope is that you don't have to define any of the organizing facets. The inheritance model is simply understood by the language directly from nesting of scopes. The way it works is if a reference is defined in an outer scope it is available (immediately) to an inner scope. You just directly call the outer reference as though it were local to the nested scope. The advantage is that you can still establish a public/private model of organization, reuse and sharing, and inheritance (of scope) without doing anything more than nesting the scopes.
The lexical scope chain is the primary means of reference resolution in JavaScript and the prototype chain is a secondary means of reference resolution. When a reference is encountered (at execution time) it must be resolved, which means finding where it was declared and what its value is. The lexical scope chain is searched first and if the reference cannot be found the prototype chain is searched. The total difference in performance is likely negligible because the JIT engine will attempt to cache the declaration point as much as possible. The primary performance difference is noticed from having less code to execute (and fewer references) to achieve the same result.
My thoughts are if I can get away with writing (and maintaining) substantially less code then any performance benefits are merely serendipity. Fortunately, writing less code happens to mean faster execution almost universally.
This was exactly my thought process the whole time. There is an approach to object creation where you return an interface of the object, and it forces you to write amazing code, and it completely does away with new, this, Object, and bind. Call and Apply are solved by just being smart about how you pass objects around to functions.
On top of this, it lets you build objects in a way that each object only has EXACTLY what it needs to get it's job done, and absolutely no more. It's a completely different type of inheritance, and it works with JS so well.
OOP code tends to require substantial boiler plate. Any speed improvement you notice likely comes from two things:
various mappings to references in memory (memory conservation and caching)
JIT optimizations
The memory reuse architecture for OOP in this language are prototypes. ES6 classes even desugar to prototypes. Prototype resolution is secondary to normal reference resolution in the lexical scope chain.
I prefer to code to the scope chain only (architecture of nested scopes). From what I have seen this approach is the fastest to execute in this language, but it has some minor draw backs with garbage collection. Since (prior to ES6) JavaScript does not have anything like a standard IO or module system a purely lexical approach tends to produce large functions or an object containing various large functions. Larger objects take longer for garbage collection to remove.
Using the memory allocation tool for Chrome from Paul Irish I can see that my purely lexical application is garbage collected just fine, but it takes longer to collect between code runs. If code runs too frequently against a large enough input memory consumption continues to rise until execution frequency slows. OOP code is likely have this problem because it is less likely to squeeze everything into a common scope, or collections that come together to share a common scope.
I tend to not worry about memory, though, in this language because its not something you have any control over. Typically its something that don't have any visibility on. Execution time, though, is very simple to profile, observe, and reduce.
I also try not to depend on jQuery when things can be done using native JavaScript. What I was trying to say is jQuery uses the above APIs so much that they will be used anyways if someone decides to use jQuery.
I attempted a guide at my approach, which somewhat explains it by example. I need to do some work to make architectural application of this approach more clear. I plan on updating that guide over the next couple of days.
I'd argue you are being overly pedantic as few people really mean never when they say never (and same for always)...tends to be "never user this unless you can't get around it".
Under that context, I agree with him/her: I don't use 'new' unless there is no other way, and anytime I see a 'new' being required I chalk that up as another JavaScriptism that was f'ed up and should have been done another way.
What is fucked up is having to write defensive code to cater for the dickheads who decide that a keyword in the language isn't worth bothering with when instantiating a NEW instance of something.
-1
u/[deleted] Mar 09 '15
I disagree about call and apply though. I prefer to never use:
new
this (one exception)
Object.create
bind
call
apply
When I avoid this list my code tends to be smaller and faster to execute. You don't need OOP in this language, thanks to lexical scope, to achieve decomposable reuse and reference sharing.
The one exception for this is event handlers where it is commonly necessary to access the event target without access to an explicit reference.