LECTURE-4 XML JavaScript Object Notation JSON Cookies Miscellaneous What Javascript can t do. OOP Concepts of JS 1
XML EXTENDED MARKUP LANGUAGE XML is a markup language, like HTML Designed to carry data Not to display data XML tags are NOT predefined. Unlike HTML You must define your own tags Self-descriptive Represented in plain text. 2
A SIMPLE EXAMPLE <note> <to>tove</to> <from>jani</from> <heading>reminder</heading> <body>let s meet tomorrow!</body> </note> Note: 1. User defined tags. 2. Self descriptive 3
JSON Text based Very useful in transferring text data over the web Language independent Used in JS, Java, PHP, etc. Provides easy means to Define JS objects Can convert JS objects to strings and vice-versa Different languages have functions for conversion. 4
JSON EXAMPLE var person = { firstname : "John, lastname : "Doe"; age : 50, address : { street : 11 Broadway, city : New York City } }; Can access data of individual fields person.firstname (or) person[firstname] person.lastname (or) person[lastname] person.address.street(or) person.address[street] 5
JSON DATA TYPES A JSON object member can be of type Number String Another JSON object Nested JSON objects Boolean Array Null Values of objects members can be Modified. E.g., person.address[street] = 2 Coumbia Way Deleted E.g., delete person.age; 6
JSON DATA CONVERSION TO STRING JSON object to string conversion var personstring = JSON.stringify(person) JSON string to an JSON object var person = JSON.parse(personString); Useful in sending JS objects over HTTP as strings. 7
JSON VS. XML Similarities Self describing and text based. Have user defined tags (unlike HTML) Nested Can be parsed in many languages Can be fetched using XMLHTTPResponse (AJAX). Differences JSON can be parsed by JS, XML can be parsed by XML parser JSON does not have an end tag (e.g., NO </firstname>) JSON can use arrays JSON is less verbose 8
COOKIES Small amount of information a web server stores on a browser. Cookie structure <name, value> pairs Typically used to Remember login and password User preferences Web sites visited Personalization Location where cookies are stored Different for each browser. Cookies have an expiration time Cookies can be removed 9
COOKIES CONTD. Cookies <name, value> pairs store Name of the cookie Server name and path If the path is /, cookie is valid in the entire domain Expiry Thursday, February 8, 2018 Each web server Can read its OWN cookies when the web page is loaded. NOT cookies of some other web server Can load multiple (up to a finite limit) cookies on each browser. 10
COOKIES CONTD. Cookies are plain text files. can t be used to read other data on the computer. are not executable files Cannot erase data on computer A site can open ONLY cookies it owns Cookies are set using Set-Cookie attribute in HTTP. 11
WHAT JAVASCRIPT CANNOT DO Javascript cannot Read or write files on client (Other than cookies). Close a window it did not open. Access information (cookies or web content) of other web pages. Access databases, without the use of AJAX and a server side script Cannot write files to servers without the help of server side script. 12
OOP FEATURES Main OOP concepts Treat real world entities as objects Has data and methods Importance features of OOP Data encapsulation Inheritance Polymorphism JS supports these OOP features But note: JS is a weakly typed language. Implementation of these features Different from strongly typed languages like C++ and JAVA 13
CREATING JS OBJECTS Create an instance of an object directly p1 = new Object( ); // Create an object directly using new p1.firstname = "John"; // Set data variables p1.lastname = "Doe"; p1.age = 50; p1.eyecolor = "blue ; p1.incrementage = changeage; // Set method p1.incrementage( ); // Call method function changeage( ) // Function definition { this.age++; } Note: There is NO class keyword, as in C++, JAVA 14
CREATING JS OBJECTS COND. Crate using a template use function // Template (class) definition function person (firstname, lastname, age, eyecolor) // Constructor { this.firstname = firstname; } this.lastname = lastname; this.age = age; this.eyecolor = eyecolor; this.incrementage = changeage; // Define a member function // Function definition function changeage( ) { } this.age++; // Creating a new object of person p1 = new person ( David, Miller, 50, brown ); 15
USEFUL JAVASCRIPT OBJECTS String Array Boolean Date Math http://w3schools.com/jsref/ 16
DATA ENCAPSULATION Data encapsulation is achieved using C++: public, private protected Java: public, private JS public accessible to class/external members private accessible to private/privileged members Privileged methods Can access private functions Can access and change private data External methods can access private members of class Something like public access functions of C++, JAVA 17
PUBLIC MEMBERS // Public data member definition function public_fn_eg ( ) { this. publicmember = <value>; } // Public function definition public_fn_eg.prototype.pubfn = function (<params>) { // code } 18
PRIVATE MEMBERS function private_fn_eg ( ) { // private data members var privatemember = <value>; //private functions function privatefunction_1 (<params>) { // code } } var privatefunction_2 = function(<params>) { // code } 19
PRIVILEGED FUNCTIONS function privileged_fn_eg { this.privilegedfn = function( ) { // CAN access private functions // CAN access/change private data } } 20
INHERITANCE Define parent and child template functions as before. To define the inheritance, use child.prototype = new parent; Children do NOT have access to parent s private members. 21
POLYMORPHISM Inherently supported in Javascript Any object calls member function in the most specific template class. Child objects call member functions From the child class if defined in child objects. From the parent class, otherwise. Parent objects calls the function from the parent template class. 22