Powerful JavaScript OOP concept here and now. CoffeeScript, TypeScript, etc

Similar documents
Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

JavaScript: the language of browser interactions. Claudia Hauff TI1506: Web and Database Technology

JavaScript: Sort of a Big Deal,

CS 251 Intermediate Programming Inheritance

CMSC 132: Object-Oriented Programming II

Class, Variable, Constructor, Object, Method Questions

Rules and syntax for inheritance. The boring stuff

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

More on JavaScript Functions

Node.js Training JavaScript. Richard richardrodger.com

What is Inheritance?

Object Model Comparisons

Microsoft Visual Basic 2005: Reloaded

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

Java: introduction to object-oriented features

1 Shyam sir JAVA Notes

Object Model. Object Oriented Programming Spring 2015

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Inheritance and Encapsulation. Amit Gupta

CS260 Intro to Java & Android 03.Java Language Basics

Java. Classes 3/3/2014. Summary: Chapters 1 to 10. Java (2)

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

FALL 2017 CS 498RK JAVASCRIPT. Fashionable and Functional!

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

The Prototype Framework Part III: Better OOP

Inheritance. Transitivity

COMP 110/L Lecture 19. Kyle Dewey

The course is supplemented by numerous hands-on labs that help attendees reinforce their theoretical knowledge of the learned material.

JavaScript: Features, Trends, and Static Analysis

Java Enum Java, winter semester ,2018 1

JavaScript: Coercion, Functions, Arrays

First IS-A Relationship: Inheritance

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

Midterm Exam CS 251, Intermediate Programming March 6, 2015

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

Inheritance (Outsource: )

Programming overview

Javascript Arrays, Object & Functions

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Programming Paradigms in Python

1 Method Signatures and Overloading (3 minutes, 2 points)

React. HTML code is made up of tags. In the example below, <head> is an opening tag and </head> is the matching closing tag.

Overview. Elements of Programming Languages. Objects. Self-Reference

JAVA MOCK TEST JAVA MOCK TEST II

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Data Abstraction. Hwansoo Han

CSE : Python Programming

Object Model. Object Oriented Programming Winter

Data Structures (list, dictionary, tuples, sets, strings)

TypeScript. Types. CS144: Web Applications

Object Oriented Programming Part II of II. Steve Ryder Session 8352 JSR Systems (JSR)

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

Client-Side Web Technologies. JavaScript Part I

JavaScript. Training Offer for JavaScript Introduction JavaScript. JavaScript Objects

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

JavaScript CS 4640 Programming Languages for Web Applications

COP 3330 Final Exam Review

STRUCTURING OF PROGRAM

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Java Object Oriented Design. CSC207 Fall 2014

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

Modelica Change Proposal MCP-0019 Flattening (In Development) Proposed Changes to the Modelica Language Specification Version 3.

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

CS304 Object Oriented Programming Final Term

JavaScript CS 4640 Programming Languages for Web Applications

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

Get Unique study materials from

Inheritance, and Polymorphism.

INTRODUCTION TO JAVASCRIPT

Programming II (CS300)

JavaScript or: How I Learned to Stop Worrying and Love a Classless Loosely Typed Language

Inheritance, Polymorphism, and Interfaces

Semantic Analysis. Lecture 9. February 7, 2018

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

Programming II (CS300)

More On inheritance. What you can do in subclass regarding methods:

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

Inheritance and Polymorphism

DC69 C# &.NET DEC 2015

PROGRAMMING III OOP. JAVA LANGUAGE COURSE

Symbols. accessor properties, attributes, creating, adding properties, 8 anonymous functions, 20, 80

CMSC 132: Object-Oriented Programming II

A- Core Java Audience Prerequisites Approach Objectives 1. Introduction

Computer Science II (20073) Week 1: Review and Inheritance

Passing arguments to functions by. const member functions const arguments to a function. Function overloading and overriding Templates

The results for a few specific cases below are indicated. allequal ([1,1,1,1]) should return true allequal ([1,1,2,1]) should return false

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Inheritance: Definition

Chapter 12. OOP: Creating Object-Oriented Programs The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

PROGRAMMING LANGUAGE 2

Basics of Inheritance

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Super-Classes and sub-classes

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

More Relationships Between Classes

Transcription:

Powerful JavaScript OOP concept here and now. CoffeeScript, TypeScript, etc JavaScript EasyOOP Inheritance, method overriding, constructor, anonymous classes, mixing, dynamic class extending, packaging, static methods and variables Zebkit February, 2016

Simple concept Zebkit classes has to be declared with zebkit.class(...) method. The method gets the following parameters: inherited class (optionally), interfaces (optionally) and array of methods the class has to have. Class can define one constructor. Constructor is a method that doesn t have a name. zebkit.class(...) method has to be used to declare zebkit classes Pass array of methods that the given class has to have // Declare MyClass class var MyClass = zebkit.class([ // Declare class constructor function () { this.a = 10; // Init variable a }, // Declare class method abc function abc() {... } // Instantiate the class var myclassinstance = new MyClass(); // Call method abc myclassinstance.abc(); myclassinstance.a; // 10 Empty name is reserved for constructor

Access to parent context All zebkit classes get special method $super(...) that has to be used to access parent class methods implementations. The big advantage of zebkit $super(...) usage is: it works properly for any combination of overridden class methods on every hierarchy level. Zebkit is smart enough to find a proper chain of super methods execution. Use $super(...) method to call a parent method implementation $super(...) works correct for deep hierarchy tracking properly an order of methods execution function(a) { this.vara = a; }, function abc() { return 2; } var B = zebkit.class(a, [ function(a, b) { // Call parent constructor this.$super(a); this.varb = b; }, function abc() { // Call parent method abc() return 3 + this.$super(); } var a = new A(3), b = new B(2, 5); a.vara; // 3 b.vara; // 2 b.varb; // 5 b.abc(); // 3 + 2 = 5

Inheritance Zebkit easyoop supports single parent inheritance. Methods of a parent class can be overridden. According to JS concept methods cannot be overloading since number of arguments can be vary. Zebkit class constructor is also inheritable. Pass a parent class to be inherited as the first parameter of Class() Class methods can be overridden, but not overloaded Use zebkit.instanceof() operator instead of standard JS instanceof // Define class A with class method // abc() function abc() {... } //Declare class B that inherits class A var B = zebkit.class(a, [ // Instantiate the class B and A var b = new B(), a = new A(); b.abc(); // Call inherited method zebkit.instanceof(b, A) // true zebkit.instanceof(b, B) // true zebkit.instanceof(a, B) // false

Mixing (Interface) Mixing is way to share common functionality between classes without necessity to support improper multi inheritance OOP concept. In zebkit mix is an interface that can declare number of methods to be mixed with a class. Interface itself cannot inherit other interfaces and classes. Mixing is done with zebkit.interface zebkit.interface can declare set of methods Interface cannot inherit other interfaces and classes Interface cannot be instantiated // Declare Mix1 and Mix2 interfaces var Mix1 = zebkit.interface([ function abc() {... } var Mix2 = zebkit.interface([ function cde() {... } // Declare class A that mixes Mix1 // interface methods var A = zebkit.class(mix1, [... // Declare class B that mixes Mix1 // and Mix2 interfaces methods var B = zebkit.class(mix1,mix2 [... // Instances of A and B classes have // got mixed methods var a = new A(), b = new B(); a.abc(); b.abc(); b.cde();

Anonymous classes Anonymous classes is one of the greatest feature zebkit EasyOOP provides. Zebkit allows developers to customize classes on the fly: during the classes instantiation you can override the class methods, constructor and extend it with new methods and variables. Pass as the last argument array of anonymous class methods to customize it Original class methods and constructor can be overridden with anonymous Super context is accessible from anonymous class // Declare class A function(p) { // Constructor this.v = p; }, function abc() { return 10; } // Instantiate anonymous class A that // overrides constructor and method // abc().add new class method- cba() var a = new A(1/* Constructor arg */,[ function(p){ // Override this.$super(p + 1); }, function abc() { // Override return this.$super() + 10; }, function cba() {... } a.abc(); // 20 a.v; // 1 + 1 = 2

Static methods and variables Zebkit static methods and variables have to be defined inside special $clazz() method that is called during class definition. The method is called in a class scope context. Static method and variables are inheritable except the variables and methods whose names start from $ character. Static variables and methods have to be declared inside $clazz() Static variables are inheritable Static variables whose names start from $ are not inheritable // Declare static stuff inside the // method function $clazz() { this.staticvar = 10; this.$staticvar = 11; this.staticmethod = function(){... }; } // Declare class B that inherits class A var B = zebkit.class(a, [ A.staticVar; // 10 A.$staticVar; // 11 B.staticVar; // 10 (inherited) B.$staticVar; // undefined B.staticMethod == A.staticMethod;// true

Packages and namespaces It is recommended to use package system zebkit provides. The packages are organized as hierarchy (the same way Java does it) and they are accessible from predefined zebkit global namespace using dot notation You also can define new namespaces with own packages hierarchy. Declare a package variables and classes safely with zebkit.package(...) A new global namespace can be declared via zebkit.namespace(...) method Use dot notation to access package stuff // Define stuff in test package zebkit.package( test, function() { this.v = Hello 1 ; }); // Access v from package test zebkit.test.v; // Hello 1 //Alternative way to access the variable zebkit( test ).v; // Hello 1 //Define own namespace and package there var ns = zebkit.namespace( MyNS ); ns.package( test, function() { this.v = Hello 2 ; }); ns.test.v; // Hello 2 // Alternative way to access namespace // variables and methods namespace( myns ).test.v; // Hello 2

Package stuff in your scope Not always handy to access classes and variables by pointing full path to its ( zebkit.ui.grid.grid ). Zebkit helps importing fields from the given package(s) into a local scope by using zebkit.import(...) that should be used in combination with JS eval() method. Import has no effect for fields whose name start from $ character. Wrap zebkit code with zebkit.ready(...) Package fields are reachable via dot notation zebkit.import() helps to import package stuff in current scope // Define variables in test package zebkit.package( test, function() { this.variable = Hello 1 ; this.a = zebkit.class([... this.$variable = 100; }); // Use zebkit.ready() to write // zebkit code safely zebkit.ready(function() { // Import fields from test // package into current scope eval(zebkit.import( test )); }); // Now access variables directly variable; // Hello 1 new A(); // new instance of class // zebkit.test.a $variable;// undefined

Dynamic class extensions Zebkit classes can be extended dynamically either on the class level (extend class definition with new methods and variables) or on the class instance level. It can be done with special extend(...) method. The method gets array of methods the class has to be extended as its argument. Use extend(...) method to extend a class or a class instance Extending of a class causes his new instances will get the extensions Extending an instance has effect only for the given class instance function abc() { return 10; } // Extend class A with cba() method // and override abc() method A.extend([ function cba() {... }, function abc() { return this.$super() + 1; } var a = new A(), aa = new A(); a.cba(); // Call cba() method // Extend instance of class A a.extend([ function q(){this.abc(); this.cba();} a.q(); // call q() instance specific // method aa.q; // undefined

Class methods via prototype With zebkit you can define a class methods and variables following the traditional way - by adding appropriate fields to the class prototype field. Do it inside special $prototype() method that is called in the class prototype scope. Use $prototype() method to add class fields Don t declare complex typed variable in prototype since it is shared between all class instances Methods declared in prototype don t have access to super class context // Declare class A // Old school - defining class // methods and variables via // prototype function $prototype() { // Declare variable v this.v = Hello 1 ; } // Declare class method abc this.abc = function() { return Hello 2 ; }; var a = new A(); a.v; // Hello 1 a.abc(); // Hello 2