Nice JavaScript Simple Introduction to object-oriented Introduction 1th/2 page _js Object-oriented

Source: Internet
Author: User
1 How to create an object:
1. Use constructor, for example:
var obj = new Object ()//var can be omitted
var obj = new Date ()

2. Use object literal value (literals),For example:

Program code

var obj = "123"//Create a String object
var obj =/^abc$///Create a RegExp object


More complicated, we can directly generate a custom object that has only attributes:

Program code

var obj = {
Name: "Killercat",
Home: "Www.i170.com/user/killercat"
}
document.write (obj.name+ "<br/>")
document.write (Obj.home)
Results:
Killercat
Www.i170.com/user/killercat





2 Properties in javascript:
str = "Www.i170.com/user/killercat"//STR a reference to a string object
document.write (Str.length)
With the reference to the object plus the "." Plus the property name, you can access this property, or you can modify this property, or even add a property, such as:
var obj = new Object ()
Obj.name = "Killercat"//Add a property directly to the object
document.write (Obj.name)//Access to object properties
Obj.name = "Kcat"//Modify object's properties
document.write (Obj.name)

Enumeration Property values:
Use a for ... in statement to enumerate attributes (specifically, enumeration property names), as mentioned earlier, such as
For (ele in window) {
document.write (ele+ "<br/>")
}
How do I get a property value?
obj = new Object ()
OBJ.P1 = "a"
OBJ.P2 = "B"
OBJ.P3 = "C"
for (ele in obj)
document.write (Obj.ele)//This is a novice may make a mistake, Obj.ele value is undefined
You should access property values like this:
document.write (eval ("obj.") +ele))

Undefined attribute:
obj = new Object ()
document.write (Obj.name)
The result: undefined

To delete an attribute:
obj = new Object ()
Obj.name = "Killercat"
Delete Obj.name
document.write (Obj.name)
The result: undefined

Understanding attributes:
We know that in java,c++, a property is either a class (class attribute or static property), or an object, that is, the object of the same class must have the same attribute, but JavaScript is not the same object, but it can have different properties. In addition to the properties of this class, there are static properties (variables) in JavaScript.

3) constructor
For some unknown reason, some people seem unwilling to use JavaScript to refer to the word classes, instead of "Object type types", and even some people call the function directly, so you can see the saying: "We have a predefined function, we produce an object ”。 This article uses the class, the noun.
JavaScript defines a method in exactly the same way as a class is defined:
function User (Name,sex) {//defines class user
THIS.name = name;
This.sex = sex;
}

user = new User ("KC", "Man")
document.write (user.name+ "<br/>" +user.sex)

The function of Contructor is to initialize attributes (variables)
Current 1/2 page 12 Next read the full text
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.