Friday, May 1, 2015

Revealing the Inner Workings of JavaScript’s “this” Keyword

To know a programming language doesn’t mean that you understand it or are using it properly. It’s the same with JavaScript. Although it’s an easy language to learn, there are many pitfalls for novices, and even for seasoned programmers.

One thing that confuses inexperienced developers is how the this keyword works. Put simply, this is a referencing alias—it’s just knowing what exactly it references, that is the tricky part.

This article aims to dispel the confusion and offer an insight into the inner workings of the this keyword.

So, What is this Anyway?

In a nutshell, this is a special identifier keyword—automatically defined in the scope of every function—pointing to the “owner” of the function being executed. But, to fully grasp its tricky nature, we need to answer two key questions:

How is this Created?

Each time a JavaScript function is invoked, a new object is created containing information about which parameters were passed, how the function was invoked, where the function was called from, and so on. One of the main properties of that object is the this reference, which is automatically bound to the object of which the function is a method.

Note: for the curious, this is detailed in §10.4.3 of the ECMAScript Language Specification and the sections which that links to.

[code language="js"] var car = { brand: "Nissan", getBrand: function(){ console.log(this.brand); } }; car.getBrand(); // output: Nissan [/code] Try it out in JS Bin

In this example this, used in this.brand, is a reference to the car object. So, this.brand is the same as car.brand.

What Does this Refer to?

The value of this, passed to all functions, is based on the context in which the function is called at run-time. The scope of this isn't concerned with how and where functions are declared, but rather where they are called from (i.e. the context).

Every line of JavaScript code is run in an execution context. The object that this refers to is redefined every time a new execution context is entered and remains fixed until it’s shifted to a different context. To find the execution context (and this binding) we need to find the call-site—the location in the code where a function is called from (not where it’s declared).

Let’s demonstrate this in the following example:

[code language="js"] var brand = 'Nissan'; var myCar = {brand: 'Honda'}; var getBrand = function() { console.log(this.brand); }; myCar.getBrand = getBrand; myCar.getBrand(); // output: Honda getBrand(); // output: Nissan [/code] Try it out in JS Bin

Even though both myCar.getBrand() and getBrand() point to one and the same function, the value of this is different because it’s based on the context in which getBrand() is being called.

As we already know, within a function, this is bound to the object of which the function is a method. In the first function call, the object is myCar, while in the second, the object is window (getBrand() is the same as window.getBrand()). So, a different context yields different a result.

Continue reading %Revealing the Inner Workings of JavaScript’s “this” Keyword%


by Ivaylo Gerchev via SitePoint

Atticus – Minimal & Personal WordPr

Atticus is a WordPress Blog Theme in Elegant and bold Design. It is perfect for any personal blog. It follows the trendy hipster design with a combination of pure elegance. With Atticus you get right to the point, no unnecessary Slideshows and animat


by csreladm via CSSREEL | CSS Website Awards | World best websites | website design awards | CSS Gallery

Web Design and Development Company

Elite Infoworld provide services in Ecommerce, open source development, PHP development, Mobile Application, Web Design Services and Internet Marketing services.


by csreladm via CSSREEL | CSS Website Awards | World best websites | website design awards | CSS Gallery

Alex Porter

Alex Porter Personal Website


by csreladm via CSSREEL | CSS Website Awards | World best websites | website design awards | CSS Gallery

This week's JavaScript news, issue 230

Happy birthday JavaScript, 20 years old this month
Read this e-mail on the Web
JavaScript Weekly
Issue 230 — May 1, 2015

We're planning a bigger feature on this soon, but it was 20 years ago this month that Brendan Eich created JavaScript (then codenamed Mocha) in just 10 days, leading to its eventual release as LiveScript in Netscape Navigator 2.0. Congratulations go not just to Brendan but the entire JavaScript scene and all it has produced over 20 amazing years for the Web and its role in our daily lives.
- Peter Cooper

ES6’s for (.. of ..) loop finally resolves some of the quirks with the alternatives when it comes to iterating over array elements.
Mozilla Hacks

A nuts and bolts introduction to getting up and running with a basic electronics experiment on the Spark platform along with Node.
Sayanee Basu

An interesting 9 minute interview with an expert JavaScript trainer.
YouTube

With new tools like Angular.js it's easy to build Single-Page Applications backed by APIs. Learn about all the major browser security issues and how you can mitigate the risks in this May 14th webinar. Sign up now.
Stormpath   Sponsored
Stormpath

A new router for Angular 2 is currently being built, but it will also be back-ported to Angular 1.4, so it’s worth checking out.
SitePoint

Vorlon.js helps you remotely load, inspect, test and debug JavaScript code running on any device with a browser. There’s also an article about why MS created it.
Microsoft

Another week, another framework, but this one looks pretty good and boasts of being incredibly modular and faster than React and Angular too.
JSBlocks

Jobs

In brief

Curated by Peter Cooper and published by Cooper Press.

Stop getting JavaScript Weekly : Change email address : Read this issue on the Web

© Cooper Press Ltd. Office 30, Lincoln Way, Louth, LN11 0LS, UK


by via JavaScript Weekly

Creating a Dating Application with Sinch: Integrating Sinch

Automated Testing of Drupal 8 Modules

In this article we are going to look at automated testing in Drupal 8. More specifically, we are going to write a few integration tests for some of the business logic we wrote in the previous Sitepoint articles on Drupal 8 module development. You can find the latest version of that code in this repository along with the tests we write today.

Drupal 8 logo

But before doing that, we will talk a bit about what kinds of tests we can write in Drupal 8 and how they actually work.

Simpletest (Testing)

Simpletest is the Drupal specific testing framework. For Drupal 6 it was a contributed module but since Drupal 7 it has been part of the core package. Simpletest is now an integral part of Drupal core development, allowing for safe API modifications due to an extensive codebase test coverage.

Right off the bat I will mention the authoritative documentation page for Drupal testing with Simpletest. There you can find a hub of information related to how Simpletest works, how you can write tests for it, what API methods you can use, etc.

By default, the Simpletest module that comes with Drupal core is not enabled so we will have to do that ourselves if we want to run tests. It can be found on the Extend page named as Testing.

Once that is done, we can head to admin/config/development/testing and see all the tests currently available for the site. These include both core and contrib module tests. At the very bottom, there is also the Clean environment button that we can use if any of our tests quit unexpectedly and there are some remaining test tables in your database.

Continue reading %Automated Testing of Drupal 8 Modules%


by Daniel Sipos via SitePoint