05 JavaScript v5 Part-4(1)
05 JavaScript v5 Part-4(1)
</body>
</html>
OOJS: Example 2
<p id="demo"></p>
<script>
var person = {
firstName: "John",
lastName : “Smith",
id : 5566
};
document.getElementById("demo").innerHTML =
person["firstName"] + " " +
person["lastName"];
</script>
OOJS: Example 3
<p id="demo"></p>
<script>
var person = {
firstName: "John", lastName : “Smith",
id : 5566,
fullName : function() {
return this.firstName + " " +
this.lastName;
}
};
document.getElementById("demo").innerHTML =
person.fullName();
OOJS: Example 4
<p id="demo"></p>
<script>
var person = new Object();
person.firstName = "John";
person.lastName = "Doe";
person.age = 50;
person.eyeColor = "blue";
document.getElementById("demo").innerHTML =
person.firstName + " is " + person.age + "
years old.";
</script>
OOJS: Using an Object Constructor
• to create many objects of one type
<p id="demo"></p>
<script>
function person(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
}
var myFather = new person("John", "Doe", 50, "blue");
var myMother = new person("Sally", "Rally", 48, "green");
document.getElementById("demo").innerHTML =
"My father is " + myFather.age + ". My mother is " +
myMother.age;
</script>
OOJS: The this Keyword
• this, is the object that "owns" the JavaScript
code.
– when used in a function, is the object that "owns"
the function
– when used in an object, is the object itself
– in an object constructor does not have a value. It
is only a substitute for the new object
• this will become the new object when the constructor
is used to create an object
OOJS: Mutable
• addressed by reference, not by value.
• var x = y; // This will not create a copy of y.
– Both x and y points to the same object.
<p id="demo"></p>
<script>
var person = {firstName:"John", lastName:“Smith", age:50,
eyeColor:"blue"}
var x = person;
x.age = 10;
document.getElementById("demo").innerHTML =
person.firstName + " is " + person.age + " years old.";
</script>
OOJS: Prototypes
• Every JavaScript object has a prototype. The
prototype is also an object.
• All JavaScript objects inherit their properties
and methods from their prototype.
• The Object.prototype is on the top of the
prototype chain.
• All JavaScript objects (Date, Array, RegExp,
Function, ....) inherit from the
Object.prototype.
OOJS: Creating a Prototype
function person(first, last,
age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
}
The constructor function is the prototype for the
person objects
OOJS: Adding to your Objects
• Adding a Property to an Object
– myFather.nationality = "English";
• Adding a Method to an Object
– myFather.name = function () {
return this.firstName + " " + this.lastName;
};
OOJS: Adding Properties to a Prototype
• Adding a Property to a Prototype
– myFather.nationality = "English";
– function person(first, last, age,
eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
this.nationality = "English"
}
OOJS: Adding Properties to a
Prototype(Alternate)
• Adding a Property to a Prototype
– myFather.nationality = "English";
– function person(first, last, age,
eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
}
person.prototype.nationality
= "English";
OOJS: Adding Methods to a Prototype
• Adding a Method to a Prototype
– function person(first, last, age,
eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
this.name = function() {return
this.firstName + " " +
this.lastName;};
}
OOJS: Adding Methods to a
Prototype(Alternate)
• Adding a Method to a Prototype
– function person(first, last, age,
eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
}
person.prototype.name = function() {
return this.firstName + " " +
this.lastName;
};
JSON
• JavaScript Object Notation
• JSON is lightweight data interchange format
• JSON is language independent
– syntax is derived from JavaScript object notation
syntax, but the JSON format is text only.
– Code for reading and generating JSON data can be
written in any programming language.
• JSON is "self-describing" and easy to understand
• https://www.programiz.com/javascript/json
JSON Example
{
"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna",
"lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]
}
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna",
"lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]
JSON vs JS Objects
<h2>Create Object from JSON String</h2>
<p id="demo"></p>
<p id="demo2"></p>
<script>
var text = '{"employees":[' +
'{"firstName":"John","lastName":"Doe" },' +
'{"firstName":"Anna","lastName":"Smith" },' +
'{"firstName":"Peter","lastName":"Jones" }]}';
obj = JSON.parse(text);
document.getElementById("demo").innerHTML =
obj.employees[1].firstName + " " + obj.employees[1].lastName;
document.getElementById("demo2").innerHTML =
jsobj.employees[1].firstName + " " + jsobj.employees[1].lastName;
</script>
JSON.stringify()
• JSON is to exchange data to/from a web server.
• When sending data to a web server, the data has to
be string.
• Convert any JavaScript datatype into a string
with JSON.stringify().
QUESTIONS?