Custom JavaScript Objects JavaScript 7. Constructor Functions Custom JavaScript objects are based on functions called constructor functions Constructor.

Slides:



Advertisements
Similar presentations
Introduction to Arrays Chapter What is an array? An array is an ordered collection that stores many elements of the same type within one variable.
Advertisements

A C LOSER L OOK AT C LASSES 1. A SSIGNING O BJECTS One object can be assigned to another provided that both objects are of the same type. It is not sufficient.
BY CARA MEDWICK Thunderbird The Ford Thunderbird.
Advanced JS The World's Most Misunderstood Programming Language ) Douglas Crockford( Shimon Dahan
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
1 Lab Session-3 CSIT221 Spring 2003 b Group Worksheet 3 Exercise (Demo Required) b No new lab demo will be assigned to allow you to focus on HW#1.
25-Jun-15 JavaScript Language Fundamentals II. 2 Exception handling, I Exception handling in JavaScript is almost the same as in Java throw expression.
Inheritance Review/Recap. ClassA extends ClassB ClassA now inherits (can access and use) all public and protected elements of ClassB We can expect the.
Writing a Class (defining a data-type). Create a new project : Project (uncheck the “Create Main Class”)
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.
JavaScript Objects Objects – JavaScript is an object-based language Has built-in objects we will use – You can create your own objects We concentrating.
Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together.
4.1 JavaScript Introduction
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
Introduction to JavaScript 11 Introduction to Programming the WWW I CMSC Winter 2004 Lecture 14.
1 CS 21A Beginning JavaScript Programming Project 5 Using Objects to Create a Shopping Cart Application Sonny Huang.
Objects Objects are just a way of representing data. They provide a way to organize a collection of data into a single unit. If objects are nouns, adjectives.
1 Understanding Inheritance COSC 156 C++ Programming Lecture 8.
Ford 1. Ford 2 Ford 3 Ford 4 Ford 5 Ford 6 Ford 7.
1 JavaScript. 2 What’s wrong with JavaScript? A very powerful language, yet –Often hated –Browser inconsistencies –Misunderstood –Developers find it painful.
Title, meta, link, script.  The title looks like:  The tag defines the title of the document in the browser toolbar.  It also: ◦ Provides a title for.
Tutorial 2 Variables and Objects. Working with Variables and Objects Variables (or identifiers) –Values stored in computer memory locations –Value can.
JavaScript, Fourth Edition
Introduction to JavaScript Gordon Tian
A way to pull together related data A Shape Class would contain the following data: x, y, height, width Then associate methods with that data A Shape.
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
1 Lecture #6 Dynamic Web Documents HAIT Summer 2005 Shimrit Tzur-David.
Object Oriented JavaScript. JavaScript Really only 4 types in JavaScript – number, string, boolean – Object (includes arrays) Remember that an object.
Functions, Objects, and Programming IDIA 619 Spring 2014 Bridget M. Blodgett.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
JavaScript Shopping Cart Yong Choi CSU Bakersfield.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
PHP Form Introduction Getting User Information Text Input.
Copyright © 2012 Pearson Education, Inc. Chapter 9 Classes and Multiform Projects.
PHP Constructs Advance Database Management Systems Lab no.3.
1 Introduction to Web Application Introduction to Java Script.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Objects © Copyright 2014, Fred McClurg All Rights Reserved.
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
Introduction into JavaScript Java 1 JavaScript JavaScript programs run from within an HTML document The statements that make up a program in an HTML.
Lesson 16. Practical Application 1 We can take advantage of JavaScript and the DOM, to set up a form so that the first text box of a form automatically.
Unit 10-JavaScript Functions Instructor: Brent Presley.
Javascript Overview. What is Javascript? May be one of the most popular programming languages ever Runs in the browser, not on the server All modern browsers.
MIT-AITI: Functions Defining and Invoking Functions Functions as Data Function Scope: The call Object Function Arguments: The arguments objects Function.
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
Object Oriented JavaScript. JavaScript Really only 4 types in JavaScript – number, string, boolean – Object (includes arrays) Remember that an object.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
My Favorite Car Stephanie Perrins COMM 115A Prof. Keating February 16, 2012.
Object Orientated Programming in Perl Simulated Models of Termites.
JavaScript Events Java 4 Understanding Events Events add interactivity between the web page and the user You can think of an event as a trigger that.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Complex & Imaginary Numbers
Functions Comp 205 Fall ‘17.
Variable Scope Variable, variable, who’s got the variable?
Constructors CMSC 202.
REBOL Writing Functions.
JavaScript Miscellany
6-3 Solving Systems Using Elimination
CS5220 Advanced Topics in Web Programming JavaScript Basics
Goals of my lectures A way of adding types DSL-making “tools”
Assessment – Java Basics: Part 1
Essential Class Operations
CS5220 Advanced Topics in Web Programming Node.js Basics
CS3220 Web and Internet Programming JavaScript Basics
JavaScript Intro.
Unit-2 Objects and Classes
Essential Class Operations
CS3220 Web and Internet Programming JavaScript Basics
CGS 3066: Web Programming and Design Fall 2019
Presentation transcript:

Custom JavaScript Objects JavaScript 7

Constructor Functions Custom JavaScript objects are based on functions called constructor functions Constructor functions have two types of elements: properties and methods A property is a variable and is consider the data of the object A method is a function(built-in or created) that is called from within the object

Constructors The following is a constructor function with four properties function car(make, model, year, owner) { – this.make = make; – this.model = model; – this.year = year; – this.owner = owner; } Keyword “this” is used to assign values to the object's properties based on the values passed to the function

Creating Objects Objects are created from constructors functions using the “new” keyword The following creates a new object named nicecar from the car constructor function and passes some arguments, which are assigned to the properties nicecar = new car (“Chevy”, “Lumina”, “1993”, “Byron”); The nicecar object now has four properties: make, model, year, and owner

Accessing an Object Property To access a property, add a period and the property name to the object Example, to access the model property of the nicecar object you type nicecar.model To display or view it, it needs to be inside a document.writeln(); – document.writeln(nicecar.model)

java7 function car(make, model, year, owner) { this.make = make; this.model = model; this.year = year; this.owner = owner;} nicecar = new car ("Chevy", "Lumina", "1993", "Byron"); nextcar = new car ("Ford", "Thunderbird", "1990", "Sam"); document.writeln(nextcar.owner); …Sam document.writeln(“my car is a” + nicecar.model); …my car is a Lumina

Adding Properties to an Object A property can be added to any object after it is created To add a property to the object, start with the object followed by a period, then the new property followed by the value function car(make, model, year, owner) { this.make = make; this.model = model; this.year = year; this.owner = owner;} nicecar = new car ("Chevy", "Lumina", "1993", "Byron"); nextcar = new car ("Ford", "Thunderbird", "1990", "Sam"); nicecar.color = "dark blue";

Adding a Property to Every Object Creating a new property for an object does not create a new property to all the object To create a property for all the objects, a prototype property is used Type the function, then prototype, followed by the new property and the value – car.prototype.color = “dark blue”; This will cause every property to be the same

Custom Objects Methods Constructor functions have two types of elements: properties and methods Until now, constructor function only contained properties A method is also a function, built-in or created A method is called from within the object After a method is created, it must be added to the constructor function by using the following – this.method_name = function_name

function car(make, model, owner) { this.make = make; this.model = model; this.owner = owner; this.display = display;} nicecar = new car ("Chevy", "Lumina", "Byron"); nextcar = new car ("Ford", "Thunderbird", "Sam"); function display( ){ document.writeln(this.make); document.writeln(this.model); document.writeln(this.owner);} nicecar.display( ); nextcar.display( );