SoFunction
Updated on 2025-04-13

Tutorial for javascript object introduction

1:Constructor method

[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]

annotation:
1: Here is a Dog object (in javascript, a function is an object, and here function Dog(name, weight) is also a constructor), and an object instance dog is created using the new keyword.
2: Where _name, _weight, _show, and _sex are the attributes of the instance dog. The instance attribute can be accessed through: instance name. attribute name or instance name ["attribute name"], that is, dog._name=dog["_name"].
3: The attributes in the constructor (here function Dog(name, weight) is also a constructor) will be attached to all instances, for example: var dog1=new Dog(…); dog1 will have _name, _weight, and _show attributes, but the _sex attribute is only proprietary to the dog instance, and it will not be loaded on other objects.
2: Object creation method:
The object object provides an easy way to create a custom object without defining the constructor.

[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]

annotation:
Here, a cat instance is created using the Object object.
3: Object initializer method

[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]

annotation:
1. Here we have actually implemented static properties and methods, and there is no need to create instances.