For more information about javascript Object operations, see. This is an entry-level tutorial.
1: constructor Method
<Script type = "text/javascript"> function Dog (name, weight) {this. _ name = name; this. _ weight = weight; this. _ show = function () {document. write (dog. _ name + "is" + dog. _ sex) ;}} var dog = new Dog ("Join", 100); dog. _ sex = "male"; dog. _ show (); script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Note:
1: A Dog object is defined here (in javascript, a function is an object. function Dog (name, weight) is also a constructor ), creates an object instance dog with the new keyword.
2: _ name, _ weight, _ show, and _ sex are the attributes of the Instance dog. You can use the Instance name. attribute name or Instance name ["attribute name"] to access the instance property, that is, dog. _ name = dog ["_ name"].
3: attributes in the constructor (function Dog (name, weight) are also constructor) will be appended to all instances, for example, var dog1 = new Dog (...); Dog1 has the _ name, _ weight, and _ show attributes, but the _ sex attribute is only private to the dog instance and will not be loaded to other objects.
2: Object creation method:
An object provides a simple method to create a custom object without the need to define constructors.
<Script type = "text/javascript"> var cat = new Object (); cat. name = "david"; cat. sex = "female"; document. write (cat. name + "is" + cat. sex); script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Note:
Here, a cat instance is created using the Object.
3: Object initializing Method
<Script type = "text/javascript"> var Cow = {name: "BeautifulCow", sex: "female", weight: 1000, show: function () {alert (this. name + this. sex + this. weight + this. address)} Cow. address = "USA. newYork "Cow. show (); script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Note:
1. Here we have actually implemented static attributes and methods without creating instances.