Modern JavaScript Develop And Design

Slides:



Advertisements
Similar presentations
Julian on JavaScript: Objects Julian M Bucknall, CTO.
Advertisements

Advanced JS The World's Most Misunderstood Programming Language ) Douglas Crockford( Shimon Dahan
Computer Science 103 Chapter 4 Advanced JavaScript.
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
Modern JavaScript Develop And Design Instructor’s Notes Chapter 2- JavaScript in Action Modern JavaScript Design And Develop Copyright © 2012 by Larry.
Taking JavaScript Seriously IS NOT THE WORST IDEA.
IS JAVASCRIPT OBJECT-ORIENTED? Contains objects Data Methods Doesn’t have classes Does have constructors Does have prototype functions Doesn’t provide.
Object Orientation An Object oriented approach views systems and programs as a collection of interacting objects. An object is a thing in a computer system.
Modern JavaScript Develop And Design Instructor’s Notes Chapter 10 – Working with Forms Modern JavaScript Design And Develop Copyright © 2012 by Larry.
1 JavaScript. 2 What’s wrong with JavaScript? A very powerful language, yet –Often hated –Browser inconsistencies –Misunderstood –Developers find it painful.
Arrays – What is it? – Creation – Changing the contents Functions – What is it? – Syntax – How they work – Anonymous functions A quick lesson in Objects.
8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming.
C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.
Tutorial 2 Variables and Objects. Working with Variables and Objects Variables (or identifiers) –Values stored in computer memory locations –Value can.
JavaScript: The Language Andrew Miadowicz Program Manager DEV307.
JavaScript, Fourth Edition
Introduction to JavaScript Gordon Tian
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 8, page 1 Sun Certified Java 1.4 Programmer Chapter 8 Notes Gary Lance
CHAPTER 4 Java Script อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
Object Oriented JavaScript. JavaScript Really only 4 types in JavaScript – number, string, boolean – Object (includes arrays) Remember that an object.
OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.
M. Taimoor Khan Javascript Objects  Every data-type defined in Javascript is an object  It has a class definition for.
Agenda Object Oriented Programming Reading: Chapter 14.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Prototypal Inheritance. Can We Do Inheritance Without Classes? How do we share behaviour across objects.
JavaScript, Fourth Edition
CNIT 133 Interactive Web Pags – JavaScript and AJAX Objects.
Associative Arrays and Objects Associative Arrays, Objects Svetlin Nakov Technical Trainer Software University
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Prototype Chain and Inheritance Prototype chain, Inheritance, Accessing Base Members Software University Technical Trainers SoftUni Team.
MIT-AITI: Functions Defining and Invoking Functions Functions as Data Function Scope: The call Object Function Arguments: The arguments objects Function.
Modern JavaScript Develop And Design Instructor’s Notes Chapter 8 – Event Handling Modern JavaScript Design And Develop Copyright © 2012 by Larry Ullman.
Object Oriented JavaScript. JavaScript Really only 4 types in JavaScript – number, string, boolean – Object (includes arrays) Remember that an object.
Chapter 5 Classes Essential C# Mark Michaelis Ch. 51 Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: ). Copyright 2010 Pearson Education,
© 2010 Robert K. Moniot1 Chapter 6 Introduction to JavaScript.
ActionScript Programming Help
ASP.NET Programming with C# and SQL Server First Edition
JavaScript, Sixth Edition
Build in Objects In JavaScript, almost "everything" is an object.
CHAPTER 10 JAVA SCRIPT.
Creating Your Own Classes
Classes (Part 1) Lecture 3
Object Oriented Programming
SPC Developer 6/10/2018 © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks.
Modern JavaScript Develop And Design
Scope, Objects, Strings, Numbers
JavaScript Syntax and Semantics
Functional Programming with Java
Modern JavaScript Develop And Design
Week 6 Object-Oriented Programming (2): Polymorphism
JavaScript: The Language
Inheritance.
Advanced Java Programming
Functions, Regular expressions and Events
CS5220 Advanced Topics in Web Programming JavaScript Basics
2017, Fall Pusan National University Ki-Joune Li
Tonga Institute of Higher Education
CS5220 Advanced Topics in Web Programming Node.js Basics
Modern JavaScript Develop And Design
Javascript Chapter 19 and 20 5/3/2019.
Strings and Dates in JavaScript
CS3220 Web and Internet Programming JavaScript Basics
Intro to Programming (in JavaScript)
CS 240 – Advanced Programming Concepts
Presentation transcript:

Modern JavaScript Develop And Design Instructor’s Notes Chapter 14 – Advanced JavaScript Modern JavaScript Design And Develop Copyright © 2012 by Larry Ullman

Objectives Create and use a namespace Define and utilize constructor functions Understand the nature of prototypical inheritance

More Objectives Recognize and begin using closures Identify an object’s type using alternative approaches Minify completed project code

Defining Namespaces var someNamespace = { someProperty: 23, someMethod: function() {…} }; // Use someNamespace.someProperty // and someNamespace.someMethod() Namespaces are named realms with their own local scope. Namespaces help to prevent conflicts. Try to use unique identifiers. Namespaces in JavaScript are created by just making a new custom object.

Creating Custom Objects var employee = { firstName: 'Joseph', lastName: 'Doe', department: 'Accounting', hireDate: new Date(), getName: function() { return this.lastName + ', ' + this.firstName; } // No comma. }; // Don't forget the semicolon! JavaScript is prototypical: you do not define classes to create custom objects. You can create a new generic object if you need a single instance of a custom object.

Creating Custom Objects function Employee(firstName, lastName, department) { this.firstName = firstName; this.lastName = lastName; this.department = department; this.hireDate = new Date(); this.getName = function() { return this.firstName + ' ' + this.lastName; }; } If you need to create multiple instances of a custom object, define a constructor function. Constructor functions are normally capitalized. Use this to refer to object attributes. Do not return values! Use new to create new objects of this type.

Creating Custom Objects Define toString() and/or valueOf() methods so that your custom objects behave like other objects in JavaScript. The two methods may or may not return the same thing.

Prototypical Inheritance var test = { thing: 1 }; test.hasOwnProperty('thing'); // true test.valueOf(); // Object: i.e., there is a valueOf() method test.hasOwnProperty('valueOf'); // false All objects in JavaScript inherit from prototypes, including custom objects. Object is the root prototype in JavaScript. Objects can be prototypes of prototypes. When you reference an attribute or method, JavaScript will look through the prototype chain to find the corresponding definition, or return undefined if not found.

Adding Prototype Methods function Employee(firstName, lastName, department) { /* Actual code. */ } Employee.prototype.getNameBackwards = function() { return this.lastName + ', ' + this.firstName: You can use the prototype property to retroactively add a method to an object type. You should be judicious about using this capability.

Closures Function definition with a memory Might be created when: One function is defined within another The inner function references variables that exist in the outer function The inner function will be called after the outer function has stopped executing Advanced concept to grasp.

Closure Example The onload function sets up the page, including creating a variable. The form’s submission function will be called any number of times, after the onload function has stopped executing. The submission function can access the variables defined within the onload function.

Closure Example window.onload = function() { 'use strict’; var target = document.getElementById('target'); var opacity = 100; var fader = setInterval(function() { opacity -= 10; if (opacity >= 0) { if (typeof target.style.opacity == 'string') { target.style.opacity = (opacity/100); } else { // For others: target.style.filter = 'alpha(opacity=' + opacity + ')'; } } else { clearInterval(fader); }, 100); // End of setInterval(). }; // End of onload anonymous function. Another closure example. The anonymous function provided to setInterval() will make use of the opacity variable.

Type Identification typeof is sometimes too vague. instanceof confirms the prototype Or you can check the constructor property.

Minifying Code Minify code before releasing it into the wild. Use a command-line or Web-based minification tool.