admin管理员组

文章数量:1201559

In JavaScript, there are two main categories of properties and methods associated with arrays: static properties/methods and instance properties/methods. These categories differ in how they are accessed and what they are used for:

Static Properties/Methods:

  1. Static Methods:

    • Static methods are called directly on the constructor function, which, in the case of arrays, is Array.
    • They are not called on specific instances of arrays but rather on the array constructor itself.
    • Static methods are used for creating new arrays or transforming data into arrays.
    • Examples of static methods include Array.of(), Array.from(), and Array.isArray().
    const newArray = Array.of(1, 2, 3); // Static method
    
  2. Static Properties:

    • Static properties are properties associated with the constructor function.
    • They are accessed using the constructor name (Array) and the property name.
    • An example of a static property is Array.length, which returns the number of arguments expected by the constructor.
    const length = Array.length; // Static property
    

Instance Properties/Methods:

  1. Instance Methods:

    • Instance methods are called on specific instances of arrays (i.e., array objects that you create).
    • They operate on the array instance and return a result based on the content of that specific array.
    • Instance methods are used for array manipulation, iteration, and other operations.
    • Examples of instance methods include push(), pop(), concat(), and forEach().
    const myArray = [1, 2, 3];
    myArray.push(4); // Instance method
    
  2. Instance Properties:

    • Array instances also have properties that provide information about the array.
    • These properties are accessed on specific arrays and provide details like the length of the array.
    • Instance properties are used to query or get information about a particular array instance.
    const myArray = [1, 2, 3];
    const length = myArray.length; // Instance property
    

In summary, the key difference is that static properties/methods are associated with the array constructor itself (Array) and are used for creating or transforming arrays and providing information about the constructor. In contrast, instance properties/methods are associated with individual array instances and are used for performing operations on specific arrays and accessing information about those arrays.

In JavaScript, there are two main categories of properties and methods associated with arrays: static properties/methods and instance properties/methods. These categories differ in how they are accessed and what they are used for:

Static Properties/Methods:

  1. Static Methods:

    • Static methods are called directly on the constructor function, which, in the case of arrays, is Array.
    • They are not called on specific instances of arrays but rather on the array constructor itself.
    • Static methods are used for creating new arrays or transforming data into arrays.
    • Examples of static methods include Array.of(), Array.from(), and Array.isArray().
    const newArray = Array.of(1, 2, 3); // Static method
    
  2. Static Properties:

    • Static properties are properties associated with the constructor function.
    • They are accessed using the constructor name (Array) and the property name.
    • An example of a static property is Array.length, which returns the number of arguments expected by the constructor.
    const length = Array.length; // Static property
    

Instance Properties/Methods:

  1. Instance Methods:

    • Instance methods are called on specific instances of arrays (i.e., array objects that you create).
    • They operate on the array instance and return a result based on the content of that specific array.
    • Instance methods are used for array manipulation, iteration, and other operations.
    • Examples of instance methods include push(), pop(), concat(), and forEach().
    const myArray = [1, 2, 3];
    myArray.push(4); // Instance method
    
  2. Instance Properties:

    • Array instances also have properties that provide information about the array.
    • These properties are accessed on specific arrays and provide details like the length of the array.
    • Instance properties are used to query or get information about a particular array instance.
    const myArray = [1, 2, 3];
    const length = myArray.length; // Instance property
    

In summary, the key difference is that static properties/methods are associated with the array constructor itself (Array) and are used for creating or transforming arrays and providing information about the constructor. In contrast, instance properties/methods are associated with individual array instances and are used for performing operations on specific arrays and accessing information about those arrays.

Share Improve this question edited Sep 26, 2023 at 11:43 samaelWebDev 32 bronze badges asked May 9, 2018 at 11:07 user9726696user9726696 3
  • Possible duplicate of Difference between Static methods and Instance methods – mohammad javad ahmadi Commented May 9, 2018 at 11:12
  • see link – mojtaba ramezani Commented Oct 10, 2019 at 5:02
  • Why exactly was the questions asked made into an answer, @samaelWebDev? Nothing wrong with it, but should be an answer. The original questions could of course be improved. But they weren't as they were obliterated for undisclosed reasons. – MiB Commented Nov 12, 2024 at 4:02
Add a comment  | 

4 Answers 4

Reset to default 15

This answer is not for Javascript, but OOP in general.

Imagine, you have a class Person. An instance of that class could be daniel.

Instance method

You could call a method on daniel, e.g: daniel.talk(), so daniel starts talking...

Static method

You could call an static method on class Person, instead of on a concrete instance, for example: Person.getPeopleFromNewYork(). Note that getPeopleFromNewYork is not related to any instance of Person but to the class itself.

(Tipically, a method like getPeopleFromNewYork() would belong to some kind of repository, but it's just for the example.)

Another illustrative examples for understand static methods are Math.sum(2, 5) or Random.randomInt()

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 ...

For Javascript specific answer please visit this link ( I found it by googling ) It seems good.

But regardless of which object oriented programming language you've chosen; generally you'd use a static method for functionalities which don't need to know other field/property status of the class. For example, converting one unit of length to another doesn't need to know what are other properties of the object. On the other hand, let's assume that we have a class Customer with properties first name and last name. Now if you needed to derive full name by concatenating first and last name you'd could create a method on class GetFullName() which doesn't take parameters and does the job for you. So on object of type Customer you could use object.GetFullName() without any parameters to get the full name. Of course, you could write a static method for the same purpose but then you'd have to pass parameters to the method. For a method which depends on a large number of parameters, it would be cumbersome.

Static and Instance Methods in JavaScript

//Constructor
var Person = function (fname, lname, country){  
    
    //private properties
    var credentials = { 
    };

    //public properties
    this.fname = fname;
    this.lname = lname;
    this.country = country;
    
    //public methods
    this.setFullName = function() {
        this.fullName = this.fname + " " + this.lname;
        console.log(this.fullName);
    }
}

// A static method: only exists on the class and doesn't exist on child objects
Person.getTime = function() {
    const dayNames = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
    console.log(dayNames[new Date().getDay()]);  
};

// An instance method: All Person objects will have this method
Person.prototype.setCountry = function(country) {
    this.country = country;  
    console.log(this.country);
}

// usecase 1
var person = new Person('Saru', 'Pooh', "Sri Lanka");
person.setFullName();
person.setCountry('Hong Kong');
person.getTime(); //Error: person.getTime is not a function
Person.getTime(); 

本文标签: Javascript Static Method vs Instance MethodStack Overflow