Javascript has two type of property and method. Static and instance both type of property and method can you use.
First of all you need to know how object can defined in javascript .. Two major way is using {} or using constructor. in {} every property and method is static and you can not create an instance of that , such as :
<script>
var person = {name:"Chayon Shaah",age:"30",say:function(){return "Hello "+this.name}}
alert(person.say()); // will allert "Hello Chayon Shaah"
</script>
just see the code , here you need to call any property or method by object reference and can't creat any instance of this person object using new keyword
and now see the constructor , how it can create an instance :
<script>
function Person(){
this.name="Chayon Shaah",
this.age = 30,
this.say = function(){
return "Hello "+this.name;
}
}
var obj = new Person();
alert(obj.say()); // will alert "Hello Chayon Shaah"
</script>
Here all of Person object properties and method can be accessed by the "obj" whice is an instance of Person object.
Person object directly can not access them , only its any instance can access them . On the other hand you can use static property or method for Person object whic can only acess by its own not by its any instance.
let's add some more instance property and static property in Person object ...
<script>
function Person(){
this.name="Chayon Shaah",
this.age = 30,
this.say = function(){
return "Hello "+this.name;
}
}
var obj = new Person();
alert(obj.say());
Person.prototype.city = "Mymensingh"; // only can be used by all instances of Person object (non static property).
alert(obj.city); // Will alert "Mymensing" (Calling via instance of Person)
Person.location = "Bangladesh"; //This is a static property which can not be accessed by any instance of Person object, Only accessed by Person Object.
`alert(Person.location); // Will alert "Bangladesh" (Calling directly via` Person object)
</script>
use "prototype" keyword for making any instance method or property with the main object name, If you want to make static property and method then just use the name without use "prototype" keyword like the example .
I Think it will help you ...