julian on javascript objects n.
Skip this Video
Loading SlideShow in 5 Seconds..
Julian on JavaScript: Objects PowerPoint Presentation
Download Presentation
Julian on JavaScript: Objects

Loading in 2 Seconds...

play fullscreen
1 / 21

Julian on JavaScript: Objects - PowerPoint PPT Presentation

  • Uploaded on

Julian on JavaScript: Objects. Julian M Bucknall, CTO. Ubiquitous objects. Everything that’s not a boolean , number, string, null or undefined is … an object Namely: A rrays are objects Functions are objects And objects are objects. An object is…. A hash table, or

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Julian on JavaScript: Objects' - yardley

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
julian on javascript objects
Julian on JavaScript: Objects

Julian M Bucknall, CTO

ubiquitous objects
Ubiquitous objects
  • Everything that’s not a boolean, number, string, null or undefined is … an object
  • Namely:
    • Arrays are objects
    • Functions are objects
    • And objects are objects
an object is
An object is…
  • A hash table, or
  • A collection of key/value pairs
    • The keys are strings
    • The values can be any type
      • If they’re data they’re known as properties
      • If functions, they’re known as methods
  • Passed as a reference not a value
  • NOT an instance of a class
extending objects
Extending objects
  • Objects are not fixed:
    • You can add new properties/methods
    • You can remove a property/method
      • delete(myObject.name);
    • You can redefine a property/method
creating an empty object
Creating an empty object
  • var o = {};
    • Preferred
  • var o = new Object();
    • It’s beginner stuff, no one does it this way
  • var o = Object.create(Object.prototype);
    • Just don’t
creating an object
Creating an object
  • Easy way: object literals
    • Start with open brace, end with closing brace
    • Define set of key/value pairs
      • Separate them with commas
      • Each key is a string or some identifier
      • Each value can be anything, including expressions
      • Separate key from value with colon
object literals
Object literals…
  • Are great since you can use them wherever a value is needed
object literals1
Object literals…
  • Are great for passing a set of data (parameters) to a function as a single parameter
  • Allows for default parameters
  • Needs an “extend” type function
  • Objects are NOT instances of some class
    • There are no classes in JavaScript
  • Objects can inherit from each other
    • Done through the “prototype”
prototypes reading
Prototypes: reading


  • officeNumber
  • (prototype link)


  • name
  • (prototype link)
prototypes writing
Prototypes: writing


  • officeNumber
  • (prototype link)


  • name
  • (prototype link)
object create
  • Object.create() makes a new object that inherits from (has as prototype) an existing one
prototypal inheritance
Prototypal inheritance
  • C# has classes and classes inherit from each other
  • JavaScript has objects and objects inherit from each other through the prototype
  • It does the same thing, but differently
prototypal inheritance1
Prototypal inheritance
  • Works by…
    • Defining some interesting object
    • Then creating other objects with that as prototype
      • Object.create
    • Then customizing those new objects
prototypal inheritance2
Prototypal inheritance
  • The end of the prototypal chain is always Object.protoype
  • So all objects have some inherited methods
    • toString()
    • hasOwnProperty()
  • Are objects
  • Are wacky compared to C#
    • Elements are key/value pairs
    • Key is the index as a string
    • length is always computed as “largest index” plus 1
    • No single type for values
    • Don’t use “for .. in” with arrays, use for
  • The prototype is Array.prototype
how do you identify an array
How do you identify an array?
  • typeofobj returns “object”
    • Rats!
  • obj instanceof Array
    • Pretty good, but doesn’t work if object is from another iframe
  • ECMAScript 5? Array.isArray(obj)
  • Object.prototype.toString(obj) returns "[object Array]"
creating an array
Creating an array
  • var a = new Array();
    • It’s beginner stuff, people
  • var a = [];
    • Much better, no need to declare initial size
  • var a = [1, 2, "3", true];
    • Much easier to read
array methods
Array methods
  • Defined on the Array prototype
  • Examples:
    • push/pop
    • shift/unshift
    • concat/join
    • slice/splice
    • sort
removing an element
Removing an element
  • delete myArray[index];
    • Just sets the element to undefined
  • myArray.splice(index, 1);
    • Removes the element
    • Reindexes the higher elements
    • Changes the length
Julian M Bucknall ∙ CTO ∙ DevExpress