Does Javascript Software Embrace Classes?

Does Javascript Software Embrace Classes?

Does JavaScript Software Embrace Classes? Leonardo Humberto Silva Miguel Ramos, Alexandre Bergel Nicolas Anquetil Federal Institute of Marco Tulio Valente Department of Computer Science RMoD Project-Team Northern Minas Gerais, Brazil Federal University of DCC - Pleiad Lab INRIA Lille Nord Europe [email protected] Minas Gerais, Brazil University of Chile France {miguel.ramos,mtov}@dcc.ufmg.br [email protected] [email protected] Abstract—JavaScript is the de facto programming language for support the implementation of mainstream class-based abstrac- the Web. It is used to implement mail clients, office applications, tions, including attributes, methods, constructors, inheritance or IDEs, that can weight hundreds of thousands of lines of code. hierarchies, etc. However, structuring a software around such The language itself is prototype based, but to master the complex- ity of their application, practitioners commonly rely on informal abstractions is a design decision, which should be taken class abstractions. This practice has never been the target of by JavaScript developers. In other words, the language is empirical research in JavaScript. Yet, understanding it is key flexible enough to support different modularization paradigms, to adequately tuning programming environments and structure including procedural programming (e.g., considering a system libraries such that they are accessible to programmers. In this as a set of functions, like in C), modular programming (e.g., a paper we report on a large and in-depth study to understand how class emulation is employed in JavaScript applications. We system is a set of modules that encapsulate data and operations, propose a strategy to statically detect class-based abstractions in like in Modula-2), and class-based object-oriented programming the source code of JavaScript systems. We used this strategy in (e.g., a system is a set of classes, like in Java). a dataset of 50 popular JavaScript applications available from In this paper, we report on an empirical study conducted GitHub. We found four types of JavaScript software: class-free to shed light on how often JavaScript developers modularize (systems that do not make any usage of classes), class-aware (systems that use classes, but marginally), class-friendly (systems their systems around abstractions that resemble object-oriented that make a relevant usage of classes), and class-oriented (systems classes. We found that almost 40% of the systems we studied that have most of their data structures implemented as classes). make a relevant usage of classes. Therefore, one might consider The systems in these categories represent, respectively, 26%, 36%, the adaptation to the JavaScript ecosystem of tools, concepts, 30%, and 8% of the systems we studied. and techniques widely used in class-based languages, such Index Terms—JavaScript; Class-based languages; Reverse En- as reverse engineering techniques, IDEs with class-based gineering. views, bad smells detection tools, recommendation engines and techniques to detect violations and deviations in class- I. INTRODUCTION based architectures. Furthermore, the new standard version JavaScript is the de facto programming language for the of JavaScript, named ECMAScript 6, will include syntactical Web [1]. The language was initially designed in the mid- support for classes [7]. Therefore, revealing how JavaScript 1990s to extend web pages with small executable code. Since developers currently emulate classes is a valuable information then, its popularity and relevance has only grown [2], [3]. for those that plan to use classes in their systems, according For example, JavaScript is now the most popular language at to this new standard syntax. GitHub, considering new repositories created by language. It is The main contributions of our work are as follows: also reported that the language is used by 97 out of the web’s • We document how prototypes in JavaScript are used to 100 most popular sites [4]. Concomitantly with its increasing support the implementation of structures including both popularity, the size and complexity of JavaScript software is data and code and that are further used as a template for in steady growth. The language is now used to implement the creation of objects (Section II). In this paper, we use mail clients, office applications, IDEs, etc, which can reach the term classes to refer to such structures, since they 1 hundreds of thousands lines of code . have a very similar purpose as the native classes available Despite the complexity, size, and relevance of modern in mainstream object-oriented languages. We also propose JavaScript software, only a few research effort has been a strategy to statically identify classes in JavaScript code carried on how developers effectively organize and manage (Section III). large JavaScript software systems. Specifically, JavaScript • We propose an open-source supporting tool, called is an imperative, and object-oriented language centered on JSCLASSFINDER, that practitioners can use to detect prototypes, rather than a class-based language [1], [5], [6]. and inspect classes in JavaScript software. This tool is Despite not having explicit class constructions, the prototype- described in Section III-B based object system of the language is flexible enough to • We provide a thorough study on the usage of classes 1http://sohommajumder.wordpress.com/2013/06/05/gmail-has-biggest- in a dataset of 50 popular JavaScript software available collection-of-javascript-code-lines-in-the-world, verified 11/15/2014 at GitHub (Section IV). This study aims to answer 978-1-4799-8469-5/15 c 2015 IEEE 73 SANER 2015, Montréal, Canada Accepted for publication by IEEE. c 2015 IEEE. Personal use of this material is permitted. Permission from IEEE must be obtained for all other uses, in any current or future media, including reprinting/ republishing this material for advertising or promotional purposes, creating new collective works, for resale or redistribution to servers or lists, or reuse of any copyrighted component of this work in other works. the following research questions: (a) Do developers use By manipulating the prototype property, we can define classes in JavaScript applications? (b) Do developers use a method whose implementation is shared by all object inheritance in JavaScript applications? (c) What is the instances. It is also possible to define properties shared by size of JavaScript classes? all objects of a given class, akin to static attributes in class- based languages. In listing 2, Circle includes a pi static II. CLASSES IN JAVASCRIPT attribute and a getCircumference method. It is worth noting This section lists the different mechanisms to emulate classes that getCircumference is not a method attached to the class in JavaScript. To identify these mechanisms we carefully (as a static method in Java). It has for example access to conducted an informal survey on documents available on the this variable, whose value is not determined using lexical the web, including tutorials2, blogs3, and StackOverflow scoping rules, but instead using the caller object. discussions4. We also surveyed a catalogue of five encapsulation styles for JavaScript proposed by Gama et al. [8] and JavaScript 1 // prototype property -> static attribute books targeting language practitioners [9], [10]. 2 Circle.prototype.pi = 3.14; Basically, an object in JavaScript is a set of name-value 3 pairs. Method and variable names are strings, called properties, 4 // function -> method 5 Circle.prototype.getCircumference= function () { and the values are any objects, including immediate values 6 return (2 * this.pi * this.radius); (e.g., numbers, boolean) and functions. To implement classes in 7 } JavaScript — i.e., data structures that resemble the class concept Listing 2. Using prototype to define methods and static attributes of mainstream object-oriented languages; the most common strategy is to use functions. Particularly, any function can be Prototypes are also used to simulate inheritance hierarchies. used as template for the creation of objects. In a function, the In JavaScript, we can consider that a class C2 is a subclass of standard JavaScript keyword this is used to define properties C1 if C2’s prototype refers to an instance of C1. For example, that emulate attributes and methods. Attributes are properties Listing 3 shows a class Circle2D that extends Circle with associated with any object, except functions. Methods are its position in a Cartesian plane. properties associated with inner functions. The keyword new is used to create objects for a class. To illustrate the definition of classes in JavaScript, we use 1 function Circle2D (x, y) { // class Circle2D 2 this.x = x; a simple Circle class. Listing 1 presents the function that 3 this.y= y; defines this class (lines 1-6), which includes a radius attribute 4 } and a getArea method. 5 6 // Circle2D is a subclass of Circle 7 Circle2D.prototype = new Circle(10); 8 1 function Circle (radius) { // function -> class 9 // Circle2D extends Circle with new methods 2 this.radius= radius; // property -> attribute 10 Circle2D.prototype.getX = function () { 3 this.getArea= function () {// function -> method 11 return (x); 4 return (3.14 this.radius this.radius); * * 12 } 5 } 13 Circle2D.prototype.getY = function () { 6 } 14 return (y); 7 // Circle instance -> object 15 } 8 var myCircle = new Circle (10); Listing 3. Implementing subclasses Listing 1. Class declaration and object instantiation Alternatively, the subclass

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    10 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us