JavaScript Objects http://javascriptissexy.com/javascript-prototype-in-plain-detailed-language/

Slides:



Advertisements
Similar presentations
C++ Classes & Data Abstraction
Advertisements

Svetlin Nakov Telerik Corporation
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Advanced JS The World's Most Misunderstood Programming Language ) Douglas Crockford( Shimon Dahan
More about classes and objects Classes in Visual Basic.NET.
16/22/2015 2:54 PM6/22/2015 2:54 PM6/22/2015 2:54 PMObject-Oriented Development Concept originated with simulating objects and their interactions. Adapted.
UML class diagrams (1) UML = Unified Modeling Language We use only class diagrams, not other UML diagrams Purpose: –keep OO concepts separate from implementation.
+ Java vs. Javascript Jessi Style. + Java Compiled Can stand on its own Written once, run anywhere Two-stage debugging Java is an Object Oriented Programming.
Object Oriented Software Development
Class & Object 蔡柏灃.
OBJECT ORIENTED PROGRAMMING CONCEPTS ISC 560. Object-oriented Concepts  Objects – things names with nouns  Classes – classifications (groups) of similar.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Enhancing JavaScript Using Application Programming Interfaces (APIs)
Tutorial 2 Variables and Objects. Working with Variables and Objects Variables (or identifiers) –Values stored in computer memory locations –Value can.
MSIS 670 Object-Oriented Software Engineering Week 1 Introduction to Java: Applications
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
JavaScript, Fourth Edition
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Martin Kruliš by Martin Kruliš (v1.0)1.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
MSIS 655 Advanced Business Applications Programming Week 1 Introduction to Java
Martin Kruliš by Martin Kruliš (v1.0)1.
CLASSES Python Workshop. Introduction  Compared with other programming languages, Python’s class mechanism adds classes with a minimum of new syntax.
CSCI-383 Object-Oriented Programming & Design Lecture 18.
Ruby Objects, Classes and Variables CS 480/680 – Comparative Languages.
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
Classes, Interfaces and Packages
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
21. PHP Classes To define a class, use the keyword class followed by the name and a block with the properties and method definitions Properties are declared.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
Chapter 11: Abstract Data Types Lecture # 17. Chapter 11 Topics The Concept of Abstraction Advantages of Abstract Data Types Design Issues for Abstract.
The Object-Oriented Thought Process Chapter 03
Introduction to Object-oriented Programming
Chapter 7- Inheritance.
Inheritance and Polymorphism
Microsoft Visual Basic 2005: Reloaded Second Edition
Dr. Charles W. Kann III JavaScript Objects Dr. Charles W. Kann III
Scope, Objects, Strings, Numbers
Notes on Assignment 1 Your code will have several classes, most notably the class that represents the entire list data structure, and the class.
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Inheritance Often, software encapsulates multiple concepts for which some attributes/behaviors overlap E.g. A computer (role-playing) game has: Monsters:
Understanding Inheritance
Web Systems Development (CSC-215)
Inheritance Basics Programming with Inheritance
The super Reference Constructors cannot be used in child classes, even though they have public visibility Yet we often want to use the parent's constructor.
Object-oriented Design in Processing
Lecture 22 Inheritance Richard Gesick.
Abstract Classes AKEEL AHMED.
Chapter 9 Objects and Classes
ISC321 Database Systems I Chapter 10: Object and Object-Relational Databases: Concepts, Models, Languages, and Standards Spring 2015 Dr. Abdullah Almutairi.
Modern JavaScript Develop And Design
Computer Programming with JAVA
Java – Inheritance.
CS2011 Introduction to Programming I Objects and Classes
Inheritance Inheritance is a fundamental Object Oriented concept
Chapter 11 Inheritance and Polymorphism
Object-oriented Design in Processing
Java Programming, Second Edition
Object-Oriented Programming: Inheritance and Polymorphism
Ruby Classes.
Inheritance.
CIS16 Application Development and Programming using Visual Basic.net
Chapter 11 Inheritance and Polymorphism
Object-oriented Design in Processing
CMPE212 – Reminders Assignment 2 due next Friday.
Object-oriented Design in Processing
Presentation transcript:

JavaScript Objects http://javascriptissexy.com/javascript-prototype-in-plain-detailed-language/

There are two interrelated concepts with prototype in JavaScript: First, every JavaScript function has a prototype property (this property is empty by default). You attach properties and methods on this prototype property when you want to implement inheritance.

function PrintStuff (myDocuments) { ​this function PrintStuff (myDocuments) { ​this.documents = myDocuments; } ​ // We add the print () method to PrintStuff // prototype property so that other instances // (objects) can inherit it:​ PrintStuff.prototype.print = function () { console.log(this.documents);

// Create a new object with the PrintStuff () constructor, // thus allowing this new object to inherit PrintStuff's // properties and methods.​ ​var newObj = new PrintStuff ("I am a new Object and I can print."); ​ ​// newObj inherited all the properties and methods, // including the print method, from the PrintStuff function. // Now newObj can call print directly, even though we never // created a print () method on it.​ newObj.print (); //I am a new Object and I can print.

The second concept with prototype in JavaScript is the prototype attribute. Think of the prototype attribute as a characteristic of the object; this characteristic tells us the object’s “parent”. In simple terms: An object’s prototype attribute points to the object’s “parent”—the object it inherited its properties from. The prototype attribute is normally referred to as the prototype object, and it is set automatically when you create a new object.

A constructor is a function used for initializing new objects, and you use the new keyword to call the constructor. For example: function Account () { } ​// This is the use of the Account constructor to create the userAccount object.​ ​var userAccount = new Account (); Moreover, all objects that inherit from another object also inherit a constructor property. And this constructor property is simply a property (like any variable) that holds or points to the constructor of the object. //The constructor in this example is Object () ​ ​var myObj = new Object (); ​// And if you later want to find the myObj constructor:​ console.log(myObj.constructor); // Object()​ ​ ​// Another example: Account () is the constructor​ ​// Find the userAccount object's constructor​ console.log(userAccount.constructor);

Note that the showNameAndColor method was inherited by the aBanana object even though it was defined all the way up the prototype chain on the Plant.prototype object. Indeed, any object that uses the Fruit () constructor will inherit all the Fruit.prototype properties and methods and all the properties and methods from the Fruit’s prototype, which is Plant.prototype. This is the principal manner in which inheritance is implemented in JavaScript and the integral role the prototype chain has in the process.

Collision Detection https://developer.mozilla.org/en-US/docs/Games/Techniques/2D_collision_detection

Bouncing Ball http://sixrevisions.com/html/bouncing-a-ball-around-with-html5-and-javascript/