0% found this document useful (0 votes)
23 views

Javascript Variables Have One of Two Scopes:: Global Scope Local Scope Within A Function

The document discusses JavaScript scoping and hoisting, singleton objects and global functions, and different ways to create objects including using constructors and prototypes. It covers declaring variables, object literal notation, and the 'this' keyword in methods.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Javascript Variables Have One of Two Scopes:: Global Scope Local Scope Within A Function

The document discusses JavaScript scoping and hoisting, singleton objects and global functions, and different ways to create objects including using constructors and prototypes. It covers declaring variables, object literal notation, and the 'this' keyword in methods.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Scoping and Hoisting

JavaScript variables have one of two

scopes:

Global scope
Local scope within a function

JavaScript does not support block scope


If you declare a variable inside a block, it is
hoisted
var num
= 7; to function scope
function demonstrateScopingAndHoisting() {
if (true) {
var num = 42;
}
alert("The value of num is " + num);
// Displays 42, not 7.
}

Singleton Objects and Global Functions in


JavaScript
JavaScript defines several singleton objects,

such as:
Math
JSON

JavaScript also defines global functions,

such as:
parseInt()
parseFloat()
isNan()

Creating Simple Objects


There are several ways to create new

objects in JavaScript:
var employee1 = new Object();
var employee2 = {};

You can define properties and methods on

an object:
var employee1 = {};
employee1.name = "John Smith";
employee1.age = 21;
employee1.salary = 10000;
employee1.payRise = function(amount) {
// Inside a method, "this" means the current object.
this.salary += amount;
return this.salary;
}

Using Object Literal Notation


Object literal notation provides a shorthand

way to create new objects and assign


properties and methods:
var employee2 = {
name: "Mary Jones",
age: 42,
salary: 20000,
payRise: function(amount) {
this.salary += amount;
return this.salary;
},
displayDetails: function() {
alert(this.name + " is " + this.age + " and earns " + this.salary);
}
};

Using Constructors
Constructor functions define the shape of

objects
They create and assign properties for the target
object
var Account = function (id, name) {
this.id
id;
The =
target
object is referenced by the this
this.name
= name;
keyword

this.balance = 0;
this.numTransactions = 0;

};

acc1 = new Account(1, "John");


var
Use
constructor
function to create new
var acc2the
= new
Account(2, "Mary");

objects with the specified properties:

Using Prototypes
All objects created by using a constructor

function have their own copy of the


properties defined by the constructor
All JavaScript objects, including constructors,
have a special property named prototype
Use the prototype to share function definitions
between objects:

Account.prototype = {
deposit: function(amount) {
this.balance += amount;
this.numTransactions++;
},
// Plus other methods
};

You might also like