Download presentation
1
>> Introduction to JavaScript
2
Web-Based Systems - Misbhauddin
Pop-up Boxes in JS ALERT alert("Welcome to the Class"); CONFIRM confirm("Are you sure you want to take IS-311?"); PROMPT prompt("What is XML?", ":("); Returns a value also Web-Based Systems - Misbhauddin
3
Functions in JavaScript
SYNTAX keyword Mainly used for event-handling in Web Development Can also be called from other functions (Reuse) function name() { } TRY NOW function test() { alert(“Tested”); } Tip: Make it work by calling the function Web-Based Systems - Misbhauddin
4
Functions in JavaScript
function name(parameters) { return b; } You can use as many parameters as you like Can also return values (numbers, strings, boolean) Use return statement function test(x) { alert(x); } TRY NOW Web-Based Systems - Misbhauddin
5
Objects - Introduction
JavaScript is Object-Oriented Properties They have a value Object Methods 'things that do something Example var abc = “Hello World”; abc.length; document.write(“Hello World”); method property Web-Based Systems - Misbhauddin
6
Web-Based Systems - Misbhauddin
Objects - Definition Objects Objects allow us to represent in code real world things and entities Such as a person or bank account SYNTAX TRY NOW keyword var object-name = { ………. } Create an object called “student” Web-Based Systems - Misbhauddin
7
Web-Based Systems - Misbhauddin
Properties in Objects Properties Each piece of information we include in an object is known as a property. Each property has a name, followed by : and then the value of that property SYNTAX TRY NOW keyword var object-name = { property-name: value, } Add two attributes with your name and major Note: Separate each property using a comma (,) and not a semicolon as in Java Web-Based Systems - Misbhauddin
8
Using Properties from Objects
Use the “.” dot notation Remember the length property you used with strings SYNTAX TRY NOW ObjectName.PropertyName; Display the properties from the student object using document.write() function Web-Based Systems - Misbhauddin
9
Using Properties from Objects
Use the brackets notation [] Remember the length property you used with strings SYNTAX TRY NOW ObjectName["PropertyName"] Repeat using brackets now. Display the properties from the student object using document.write() function Note: We need " " around the property's name. Web-Based Systems - Misbhauddin
10
Objects using Constructor
Uses the “new” keyword Remember the length property you used with strings Adding Properties using constructor method SYNTAX keyword var ObjectName = new Object(); ObjectName.PropertyName=value; Web-Based Systems - Misbhauddin
11
Web-Based Systems - Misbhauddin
Try Now Create an object called BMW, which should have 3 properties: a cost of “too much", speed of 220 and country of "Germany" Using Constructor Method Using Object Literal Notation var BMW={ cost:"too much“, speed:220, country:"Germany“ } var BMW=new Object(); BMW.cost="too much"; BMW.speed=220; BMW.country="Germany"; Web-Based Systems - Misbhauddin
12
Web-Based Systems - Misbhauddin
Methods in Objects Methods A method is just like a function associated with an object Each property has a name, followed by : and then the value of that property SYNTAX objectName.functionName = function (parameter) { …………………….. }; TRY NOW Create a method to change the major and set it to the attribute value Web-Based Systems - Misbhauddin
13
Methods in Objects – “this” keyword
We can make a method work for many objects using “this” keyword Refer to whichever object called that method Note: should associate with object Using the object-name Using “this” keyword student.changeMajor=function(x) { student.major=x; } var changeMajor=function(x){ this.major=x; } student.changeMajor=changeMajor; Web-Based Systems - Misbhauddin
14
Next on JavaScript Arrays Object Models Forms
Using the Document Object Model (DOM) Using the Browser Object Model (BOM) Forms Validation
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.