JavaScript Objects
JavaScript objects are data that owns properties and methods
The following code sets the value for the variable car to "Fiat":
An object is also a variable, but an object can contain multiple values (multiple variables).
var car = {type:"Fiat", Model: $, color:"white"}; |
Object Definition
var person = {firstName:' John ', LastName:"Doe", Age:eyecolor:"Blue"}; |
Accessing Object Properties
Person.lastname; |
person["LastName"]; |
Create JavaScript Object I
Creating JavaScript Objects II
Accessing Object Properties I
Accessing Object Properties II
function properties are accessed as a method
function properties are accessed as a property
JavaScript functions Local JavaScript variables
A variable declared inside a JavaScript function (using Var) is a local variable, so it can only be accessed inside the function. (The scope of the variable is local).
You can use local variables with the same name in different functions, because only the function that declares the variable can recognize the variable.
As soon as the function is complete, the local variable is deleted.
Global JavaScript variables
Variables declared outside of a function are global variables, and all scripts and functions on a Web page can access it.
The lifetime of a JavaScript variable
The lifetime of JavaScript variables begins at the time they are declared.
Local variables are deleted after the function is run.
Global variables are deleted after the page is closed.
Assigning values to undeclared JavaScript variables
If you assign a value to a variable that has not been declared, the variable is automatically declared as a global variable.
This statement:
Carname= "Volvo";
A global variable is declared carname, even if it executes within the function.
JavascriptScope
Scopes can access a collection of variables.
JavaScript Scopes
In JavaScript, objects and functions are also variables.
In JavaScript, scopes are a collection of accessible variables, objects, and functions.
JavaScript function Scope: scope is modified within a function.
JS Basic Notes