2017, Fall Pusan National University Ki-Joune Li

Slides:



Advertisements
Similar presentations
Intro to JavaScript. JavaScript History Client (generally browser-side) language invented at Netscape under the name LiveScript around 1995 Netscape wanted.
Advertisements

JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
1 CSC 551: Web Programming Spring 2004 client-side programming with JavaScript  scripts vs. programs  JavaScript vs. JScript vs. VBScript  common tasks.
Tutorial 10 Programming with JavaScript
25-Jun-15 JavaScript Language Fundamentals II. 2 Exception handling, I Exception handling in JavaScript is almost the same as in Java throw expression.
CS 299 – Web Programming and Design Overview of JavaScript and DOM Instructor: Dr. Fang (Daisy) Tang.
The Information School of the University of Washington Oct 20fit programming1 Programming Basics INFO/CSE 100, Fall 2006 Fluency in Information Technology.
WEB DESIGN AND PROGRAMMING Introduction to Javascript.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
CISC474 - JavaScript 03/02/2011. Some Background… Great JavaScript Guides: –
Week 9 PHP Cookies and Session Introduction to JavaScript.
Javascript. Outline Introduction Fundamental of JavaScript Javascript events management DOM and Dynamic HTML (DHTML)
Introduction to JavaScript Gordon Tian
Tutorial 10 Programming with JavaScript. XP Objectives Learn the history of JavaScript Create a script element Understand basic JavaScript syntax Write.
CHAPTER 4 Java Script อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
Objects.  Java Script is an OOP Language  So it allows you to make your own objects and make your own variable types  I will not be going over how.
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
M. Taimoor Khan Javascript Objects  Every data-type defined in Javascript is an object  It has a class definition for.
JavaScript Syntax, how to use it in a HTML document
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
JavaScript. JavaScript is the programming language of HTML and the Web. Easy to learn One of the 3 languages of all developers MUST learn: 1. HTML to.
10 – Java Script (3) Informatics Department Parahyangan Catholic University.
JS1-1 Introduction to JavaScript (JavaScript 1) Xingquan (Hill) Zhu
CSC 330 E-Commerce Teacher Ahmed Mumtaz Mustehsan Ahmed Mumtaz Mustehsan GM-IT CIIT Islamabad GM-IT CIIT Islamabad CIIT Virtual Campus, CIIT COMSATS Institute.
COP 3813 Intro to Internet Computing Prof. Roy Levow Lecture 4 JavaScript.
Unit 11 –Reglar Expressions Instructor: Brent Presley.
1 Javascript CS , Spring What is Javascript ? Browser scripting language  Dynamic page creation  Interactive  Embedded into HTML pages.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
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,
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
1 Objects In JavaScript. 2 Types of Object in JavaScript Built-in objects User Defined Objects Browser Object Document Object Model.
JavaScript Tutorial. What is JavaScript JavaScript is the programming language of HTML and the Web Programming makes computers do what you want them to.
Java Script Date Object
Servlets What is a Servlet?
JavaScript.
Build in Objects In JavaScript, almost "everything" is an object.
Web Systems & Technologies
Chapter 4 Client-Side Programming: the JavaScript Language
CS5220 Advanced Topics in Web Programming JavaScript and jQuery
Barb Ericson Georgia Institute of Technology May 2006
SEEM4570 Tutorial 05: JavaScript as OOP
Scope, Objects, Strings, Numbers
JavaScript Fundamentals
JavaScript Syntax and Semantics
JavaScript: Functions.
>> JavaScript: Arrays
JavaScript and Ajax (Expression and Operators)
JavaScript an introduction.
PHP.
2017, Fall Pusan National University Ki-Joune Li
Functions, Regular expressions and Events
CS5220 Advanced Topics in Web Programming JavaScript Basics
Tutorial 4 JavaScript as OOP Li Xu
JavaScript MCS/BCS.
Tutorial 10 Programming with JavaScript
CS5220 Advanced Topics in Web Programming Node.js Basics
2017, Fall Pusan National University Ki-Joune Li
2017, Fall Pusan National University Ki-Joune Li
JavaScript CS 4640 Programming Languages for Web Applications
Tutorial 10: Programming with javascript
JavaScript – Functions
The <script> Tag
Javascript data structures
PHP an introduction.
CS3220 Web and Internet Programming JavaScript Basics
Lecture 9: JavaScript Syntax
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
JavaScript CS 4640 Programming Languages for Web Applications
Presentation transcript:

2017, Fall Pusan National University Ki-Joune Li JavaScript – Objects 2017, Fall Pusan National University Ki-Joune Li

JavaScript Objects – Basic Concepts Object in JavaScript is a collection of variables with Methods, Properties (variables), Definitions, Constructor In JavaScript, almost everything is object Booleans can be objects (if defined with the new keyword) Numbers can be objects (if defined with the new keyword) Strings can be objects (if defined with the new keyword) Dates, Maths, Regular Expression, Arrays, Functions, Objects are always objects Except primitives – string, number, Boolean, null, undefined

JavaScript Objects – Basic Concepts Object has Method Properties (Attributes) Definition (e.g. macro definition) Creating an object Using a literal object new Object() operator User-defined Object constructor Built-in JavaScript Constructor – Object(), String(), Number(), Boolean(), Array(), RegExp(), Function(), Date() RegExp: Regular Expression Object – search pattern to search in a string Syntax: /pattern/modifiers Example: var patt=/w3schools/i

JavaScript Objects – Basic Concepts Primitive Types vs. Objects Some primitive types can be created as objects example: they can be compared : But NOT Recommend to create primitive as an object – Why ? Object-Object is not comparable – example var x = "John"; var y = new String("John"); <script> var x = "John"; // x is a string var y = new String("John"); // y is an object document.getElementById("demo").innerHTML = (x==y); </script>

JavaScript Objects – Basic Concepts Adding New Methods Adding methods to an object is done inside the constructor function: Example function person(firstName, lastName, age, eyeColor) {     this.firstName = firstName;       this.lastName = lastName;     this.age = age;     this.eyeColor = eyeColor;     this.changeName = function (name) {         this.lastName = name;     }; }

JavaScript Objects – String Object Many methods can be applied search can receive regular expression as well. var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; var sln = txt.length; var str = "Please locate where 'locate' occurs!"; var pos = str.indexOf("locate"); var str = "Please locate where 'locate' occurs!"; var pos = str.search("locate"); var str = "Apple, Banana, Kiwi"; var res = str.slice(7, 13) var str = "Apple, Banana, Kiwi"; var res = str.substr(7, 6); str = "Please visit Microsoft!"; var n = str.replace("Microsoft", "W3Schools"); str = "Please visit Microsoft and Microsoft!"; var n = str.replace(/Microsoft/g, "W3Schools");

JavaScript Objects – String Object var text1 = "Hello World!";       // String var text2 = text1.toUpperCase();  // text2 is text1 converted to upper var text1 = "Hello World!";       // String var text2 = text1.toLowerCase();  // text2 is text1 converted to lower var text1 = "Hello"; var text2 = "World"; var text3 = text1.concat(" ", text2); var str = "HELLO WORLD"; str.charAt(0);            // returns H var str = "HELLO WORLD"; str[0];                   // returns H NOT a good idea, NOT Safe var txt = "a,b,c,d,e";   // String txt.split(",");  txt.split("|");

JavaScript Objects – Other Objects Number Object Math Object and Math Random

JavaScript – Array Objects JavaScript supports array objects as other high level language. sort.js function start() { var a=[10, 1, 9, 2, 30, 8, 7, 4, 5, 5]; outputArray("Data Items in the original order: ", a, document.getElementById("originalArray")); a.sort(CompareIntegers); outputArray("Data Items in the sorted order: ", a, document.getElementById("sortedArray")); } function outputArray(heading, theArray, output) { output.innerHTML=heading+theArray.join(" "); // insert blank between elements function CompareIntegers(a, b) { return a-b;} window.addEventListener("load", start, false); Run start when this HTML document is loaded

JavaScript – Array Objects <!DOCTYPE html> <html> <body> <h2>JavaScript array</h2> <p>This is a sample function:</p> <script src="javaSample-array.js"></script> <!-- include js file --> <p id="originalArray"></p> <p id="sortedArray"></p> </body> </html>

JavaScript – Array Objects Find Max example. findMax.js function start() { var a=[10, 1, 9, 2, 30, 8, 7, 4, 5, 5]; var maxValue=findMax(a); outputArray("max value: ", maxValue, document.getElementById("result")); } function outputArray(heading, v, output) { output.innerHTML=heading+v; // insert blank between elements function findMax(a) { var v=-100; for(var i in a) { if (a[i]> v) v=a[i];} // for(var i=0; i<a.length;i++) return v; window.addEventListener("load", start, false);

JavaScript – Array Objects <!DOCTYPE html> <html> <body> <h2>JavaScript array</h2> <p>This is a sample function:</p> <script src="javaSample-array.js"></script> <p id=“result"></p> </body> </html>

JavaScript – Array Objects Multi-dimensional Array 10 1 9 2 30 8 var a=[[10, 1], [9, 2], [30, 8]]; 10 1 9 2 30 8 7 var a=[[10, 1], [9, 2], [30, 8, 7]]; var a=new Array(3); // allocate 2 rows a[0]=new Array(2); // allocate 2 columns to row 0 a[1]=new Array(2); // allocate 2 columns to row 1 a[2]=new Array(3); // allocate 3 columns to row 0

JavaScript Objects – Prototypes Once an object is created, we may add a new property or method. function Person(first, last, age, eyecolor) {     this.firstName = first;     this.lastName = last;     this.age = age;     this.eyeColor = eyecolor; } var myFather = new Person("John", "Doe", 50, "blue"); var myMother = new Person("Sally", "Rally", 48, "green"); myFather.nationality = "English"; myFather.name = function () {return this.firstName + " " + this.lastName; };

JavaScript Objects – Prototypes We can also add new properties or method using prototype <!DOCTYPE html> <html> <body> <p id="demo"></p> <script> function Person(first, last, age, eye) { this.firstName = first; this.lastName = last; this.age = age; this.eyeColor = eye; } Person.prototype.nationality = "English"; var myFather = new Person("John", "Doe", 50, "blue"); document.getElementById("demo").innerHTML ="My father is " + myFather.nationality; Person.prototype.name = function() { return this.firstName + " " + this.lastName; }; document.getElementById("demo").innerHTML ="My father is " + myFather.name(); </script> </body></html>