Creating a Javascript Object
In the JavaScript programming language, there are 5 data types and 3 Object types that represent references. Data types are Number, String, Boolean, Undefined, and Null (this is also an object). There are Object types that represent references such as functions, arrays, objects.
It is possible to create objects using various methods with Javascript. A few of these methods are explained with examples.
1-{} symbols are used to create objects in JavaScript.
var Personnel={};
In this way, we created an Object called Personnel with nothing inside.
Let's assign a few properties to the created object. Include the staff's name, surname, phone number and daily wage. We will use these as property: value to add them to the object. If more than one property or method will be defined, a comma (,) symbol should be placed between each value.
var Staff = {
islander",
surname:"BAK",
phone:"715 400 40 40",
fee:100
}
To define a method, it is defined as property: function, again using property: value. Here, anonymous functions are defined with JS. If one of the properties will be used in the method while defining the object (in the example we will use the fee to calculate the payment), the this keyword is used for the property in the object.
var Staff = {
islander",
surname:"BAK",
phone:"715 400 40 40",
fee:100,
feeOde:function(day){
return day*fee;
}
}
A different example is discussed below. You can copy the codes and use them by creating an HTML document.
var Student={
islander",
surname:"Al",
nameSurname:function(){
return this.name+" "+this.surname;
}
}
document.write(Student.nameSurname());
The this key is used to access parts (properties, methods) within the object. Object will be created by creating it like a function. (Functions in JavaScript language are also objects)
2-) It is also possible to define another method of creating a JavaScript object as a constructor pattern. A class is created with the function definition method, and the created class is derived as an object with the new key.
It is also possible to define it as /*function Member(){*/
var Member=function(){
this.name;
this.surname;
this.nameSurname=function(){
return this.name+" "+this.surname;
}
}
var Member1=new Member();
var Member2=new Member();
Member1.isim="Hayri";
Member1.surname="COACH";
Member2.isim="Hüseyin";
Member2.surname="ESS";
document.write(Uye1.nameSurname());
document.write("
");
document.write(Member2.NameSurname());
Example
var Member=function(){
/*
It is defined within the function with the var key. We don't have a chance to call it outside the function
We cannot call it as a member.name. Only notifications made in the form of this.changename will be called from the outside.
*/
var name;
I have a surname;
this.age=18;
/* is used to transfer an external value to the name variable defined within the function. setter method*/
this.setName=function(v){
name=v;
}
/* used to read the value in the name variable defined within the function. getter method*/
this.getName=function(){
return name;
}
this.setSurname=function(v){
surname=v;
}
/*a function defined within the class*/
this.nameSurname=function(){
return name+" "+surname;
}
}
var Member1=new Member();
Member1.setName("hayri");
Member1.setSurname("COACH");
/*The variable defined with this key is called directly.*/
Member1.age=20;
document.write(Uye1.nameSurname());
document.write("
");
document.write(Member1.getName()+" ");
document.write("
");
document.write(Member1.age);