Book Image

Object-Oriented JavaScript - Third Edition

By : Ved Antani, Stoyan STEFANOV
5 (1)
Book Image

Object-Oriented JavaScript - Third Edition

5 (1)
By: Ved Antani, Stoyan STEFANOV

Overview of this book

JavaScript is an object-oriented programming language that is used for website development. Web pages developed today currently follow a paradigm that has three clearly distinguishable parts: content (HTML), presentation (CSS), and behavior (JavaScript). JavaScript is one important pillar in this paradigm, and is responsible for the running of the web pages. This book will take your JavaScript skills to a new level of sophistication and get you prepared for your journey through professional web development. Updated for ES6, this book covers everything you will need to unleash the power of object-oriented programming in JavaScript while building professional web applications. The book begins with the basics of object-oriented programming in JavaScript and then gradually progresses to cover functions, objects, and prototypes, and how these concepts can be used to make your programs cleaner, more maintainable, faster, and compatible with other programs/libraries. By the end of the book, you will have learned how to incorporate object-oriented programming in your web development workflow to build professional JavaScript applications.
Table of Contents (25 chapters)
Object-Oriented JavaScript - Third Edition
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
Built-in Functions
Regular Expressions

JSON


The JSON object is new to ES5. It's not a constructor (similarly to Math) and has only two methods: parse() and stringify(). For ES3 browsers that don't support JSON natively, you can use the "shim" from http://json.org.

JSON stands for JavaScript Object Notation. It's a lightweight data interchange format. It's a subset of JavaScript that only supports primitives, object literals, and array literals.

Members of the JSON object

Following are the members of the JSON object:

Method

Description

parse(text, callback)

Takes a JSON-encoded string and returns an object:

    > var data = '{"hello":   1, "hi": [1, 2, 3]}';   
    > var o = JSON.parse(data);   
    > o.hello;   
    1   
    > o.hi;   
    [1, 2, 3]   

The optional callback lets you provide your own function that can inspect and modify the result. The callback takes key and value arguments and can modify the value or delete it (by returning undefined).

    > function callback(key, value)   {   
        console.log(key, value);   
        if (key === 'hello') {   
          return 'bonjour';   
        }   
        if (key === 'hi') {   
          return undefined;   
        }   
        return value;   
      }   
   
    > var o = JSON.parse(data, callback);   
    hello 1   
    0 1   
    1 2   
    2 3   
    hi [1, 2, 3]   
    Object {hello: "bonjour"}   
    > o.hello;   
    "bonjour"   
    > 'hi' in o;   
    false   

stringify(value, callback, white)

Takes any value (most commonly an object or an array) and encodes it to a JSON string.

    > var o = {   
    hello: 1,    
    hi: 2,    
    when: new Date(2015, 0, 1)   
   };   
   
    > JSON.stringify(o);   
   "{"hello":1,"hi":2,"when":
    "2015-01-01T08:00:00.000Z"}"   

The second parameter lets you provide a callback (or a whitelist array) to customize the return value. The whitelist contains the keys you're interested in:

    JSON.stringify(o, ['hello', 'hi']);   
    "{"hello":1,"hi":2}"   

The last parameter helps you get a human-readable version. You specify the number of spaces as a string or a number:

    > JSON.stringify(o, null, 4);   
    "{   
    "hello": 1,   
    "hi": 2,   
    "when": "2015-01-01T08:00:00.000Z"   
    }"