1. JavaScript Objects example
name : "Amal",
speak : function(){
return "my name is "+this.name;
},
address : {
number : 'abc',
street : '123 street',
city : 'Colombo'
}
};
console.log(Customer.speak())
console.log(Customer.address)
console.log(Customer.address.city)
2. Creating multiple, different objects of the same type
For this we can use the constructor function. Basically a constructor function allows us to get the functionality of a classes found in OOP languages. Example for a javascript constructor function is given below.
//let's create a constructor function called Person
function Person(name,age,city){
function Person(name,age,city){
this.name = name;
this.age = age;
this.city = city;
this.getInfo = function(){
this.age = age;
this.city = city;
this.getInfo = function(){
return "My name is "+ this.name + ". My age is " + this.age + ". I live in " + this.city + ".";
};
}
// let's create a person called amal
var amal = new Person('Amal',18,'Colombo');
console.log(amal.getInfo())
//let's check whether amal is an instance of Person
console.log(amal instanceof Person )
amal.mynickname = "amale"
amal.mycar = "corolla"
console.log(amal.mynickname);
console.log(amal.mycar);
//but Person.mynickname would give an error since mynickname is an instance varialbe, not static
This way custom variables which are not defined in the constructor function can be added to amal object. There variables specific only to the amal object.
};
}
// let's create a person called amal
var amal = new Person('Amal',18,'Colombo');
console.log(amal.getInfo())
//let's check whether amal is an instance of Person
console.log(amal instanceof Person )
4. Let's add an instance variable to the object we created just now (amal)
amal.mynickname = "amale"
amal.mycar = "corolla"
console.log(amal.mynickname);
console.log(amal.mycar);
//but Person.mynickname would give an error since mynickname is an instance varialbe, not static
This way custom variables which are not defined in the constructor function can be added to amal object. There variables specific only to the amal object.
5. Let's Pass an object to a function and change its value
//let's create a function and chage the name of the object amal
function changeName(personObject){
personObject.name = "Bob lee";
}
changeName(amal)
console.log(amal.getInfo());
* Objects are only going to be equal if they reference the exact same object. What the heck is that? Let's clear that out.
//let's create 2 more persons called sunil and nimal
var sunil = new Person('sunil',20,'Kandy');
var animal = new Person('sunil',20,'Kandy');
console.log(sunil === nimal);
//by referring to the same object
var sunil_same_ref = sunil
console.log(sunil === sunil_same_ref)
In JavaScript, === means we are checking for the value as well as the type of the variable and == means we are only checking for the value.
great
ReplyDeleteThe Eight-Wheel Classic - TITIAN Arts
ReplyDeleteThe eight-wheel classic bicycle 출장샵 is available https://septcasino.com/review/merit-casino/ in https://jancasino.com/review/merit-casino/ six sizes. The Bicycle Wheel is titanium metal trim a 1등 사이트 classic bicycle made in USA, but there are three variations in