In a function that is not related to an object this refers to the global object

In a function that is not related to an object this

This preview shows page 40 - 48 out of 92 pages.

In a function that is not related to an object, this refers to the global object (in non-strict mode) or undefined (in strict mode). This applies even when the function is nested inside an object’s method. greeting: function () { alert( 'Hi! I\ ’m ' + this .name.first + '.’ ); } 40
Image of page 40
Create an Object JavaScript uses special functions called constructor functions to define new objects and their features. To create an object, we use the new keyword. function Person (first, last, age, gender, interests) { this .name = { 'first' : first, 'last' : last }; this .age = age; this .gender = gender; this .interests = interests; this .greeting = function () { alert( 'Hi! I\'m' + this .name.first + '.’ ); }; } var man = new Person( 'Henry' , 'Lee' , 32, 'male' , [ 'music' , 'skiing' ]); 41
Image of page 41
Create an Object JavaScript has a built-in method called create() that allows you to create a new object based on any existing object . To create a new object, simply pass the desired prototype object as an argument to the Object.create() method. function Person (first, last, age, gender, interests) { : : } var man = new Person( 'Henry' , 'Lee' , 32, 'male' , [ 'music' , 'skiing’ ]); var boy = Object.create(man); console.log(boy.age); // => 32 console.log(boy.name.first); // => Henry 42
Image of page 42
Bracket notation In previous examples, we used the dot notation to access an object’s properties and methods. Another way to access object properties is to use bracket notation . This looks very similar to how we access the items in an array, instead of using an index number to select an item, you are using the name associated with each member's value. It is the reason why objects are sometimes called associative arrays . boy.age boy.name.first boy[ " age " ]; boy[ " name " ][ " first " ] 43
Image of page 43
Dynamically Add Object Members We can add new properties to an object during runtime. man[ 'eyes' ] = 'hazel ' ; man.farewell = function () { alert( "Bye everybody!" ); } 44
Image of page 44
The Window Object The window object is supported by all browsers. It represents the browser's window . There can be several window objects at a time, each representing an open browser window. Global variables are properties of the window object. Global functions are methods of the window object. Even the document object (of the HTML DOM) is a property of the window object. 45
Image of page 45
The Window Object The window object has a number of properties and methods that we can use to interact with it. Property /method Description document Returns the Document object for the window history Returns the History object for the window location Returns the Location object for the window status Sets or returns the text in the statusbar of a window alert() Displays an alert box with a message and an OK button close() Closes the current window focus() Sets focus to the current window open() Opens a new browser window prompt() Displays a dialog box that prompts the visitor for input resizeTo() Resizes the window to the specified width and height 46
Image of page 46
try...catch...finally Statement The try/catch/finally statement marks a block of code to try and specifies a response should an exception be caught.
Image of page 47
Image of page 48

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture