On the concept of JS object, create, call, delete, modify!

Source: Internet
Author: User

First, we often confused, what is the object, in fact, it is a kind of thinking, a sense of things, like we say
The world is a matter of material composition of the same reason, understand the following a few words! objects are not so abstract!
All things in 1.javascript are objects, strings, numbers, arrays, functions!
And JS also allows custom objects!
2, the object is just a special data type with properties and methods
3. Property is the value associated with the object, which is an action that can be performed on the object!

In fact, we should have a question to solve here, we say the function is an object, why?
Very simple, JS in all things are objects, functions are naturally included, and the function has its own property of a method, which is more proof of this!

We still have questions about the properties and methods of the function, what exactly is the function used for? Here's the answer: the function can be encapsulated, can be called, what's the concept,
With this thing, we can write a lot of common things!

The basic skill of our learning object should be learned a few points:

I. How to create an object:
1. Create directly: Person = new Object ()! Create an object directly to the person!
2. Create using the Function object constructor: In fact, it is the creation of a function (in JS, this usually refers to the function that we are executing, or the object to which the function is running!) )
function person (firstname,lastname,age,eyecolor)
// {
This.firstname=firstname;
This.lastname=lastname;
This.age=age;
this.eyecolor= function () {alert ("This is" + THIS.name + ".")};
// }
3, create with JSON!
Person={firstname: "John", LastName: "Doe", Age:50,eyecolor: "Blue"}; This style is also in the creation of objects!

The properties of the object, we can add, the method of adding the object is actually attached to the function on the object!

Two. Object creation for the second function: all operations in this way need to be new to a My object, this object is a normal object!
var my = new person ();
1. Call:
Use attribute: alert (my.firstname);
Calling method: My.eyecolor ();
2. Modification:
Modify Properties: My.age = 18;
3. Increase:
My.name = "Nihao"
4. Delete:
Delete Hero.name;


Another common example:

var hero = {
Name: "Zhangwuji",
Age:18,
tel:1388888888,
Sayme:function ()
// {
Alert ("This is Zhangwuji.");
// }
// }

1 calls
alert (hero.name);
Hero.sayme
Alert (hero[' name ');
hero[' Sayme ']

2 modifications
Hero.name = "Zhouzhiruo";
Alert (Hero.nam
Hero.sayme = function () {
Alert ("This is Zhouzhiruo.")
// }
Hero.sayme

3 Increase
Hero.value = "Zhouzhiruo";
alert (Hero.value);
Hero.sayval = function () {
Alert ("Zhangwuji ' s value is Zhouzhiruo.");
// }
Hero.sayval

4 Delete
Delete Hero.name;
alert (hero.name);
Delete Hero.sayme; Only the calling function needs to have ()
Hero.sayme
var method = {
Add:function (A, b) {
return a + B;
//     }
// }
alert (Method.add);

On the concept of JS object, create, call, delete, modify!

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.