Javascript Object getting started instance tutorial _ js object-oriented

Source: Internet
Author: User
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.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.