JavaScript Learning notes-Object basics

Source: Internet
Author: User

JavaScript Object Basics

First, the noun explanation:
1. Object-based
All objects are programmed with the concept of objects.

2. Object-oriented programming (OOP Object oriented programming)

A. Object
It is anything that people want to study, not only to express specific things, but also to represent abstract rules, plans or events.
An unordered collection of properties, each of which can have a value (original value, object, function)

B. Properties and behavior of an object
Attribute: Describes his state with a data value
Behavior: A method used to change the behavior of an object

C. Class
An abstraction of an object of the same or similar nature is a class. The abstraction of an object is a class, and the materialization (instantiation) of a class is called an object.


Second, create the object

1. Constructor Method:
function Fun1 () {

}
var obj=new fun1 ();
Alert (typeof obj);

1. Constructor method: function Fun1 () {   } var obj=new fun1 (); alert (typeof obj)

2.Object method
var obj=new Object ();

  2.Object method  var obj=new Object ();  function Object () {  alert (123);  }

3.json method (JavaScript Object notation) native format
var obj={};
Alert (typeof obj);

  3.json method (JavaScript Object notation) native format    var obj={};


Iii. How to add properties and methods
If the value of a property is a function, we call it a method of the object, otherwise called a property.

1. Construction method
A. Declarations are added later

A. Declare add    function fun1 () {    }    var obj=new fun1 () later;    Obj.name= "Zhangsan";    Obj.say=function  () {       var a= "speak";        return A;    }

B. When the declaration is added

A. When declaring, add function fun1 () {  this.name= "Zhang San";  This.eat=function  () {    alert ("I can Eat");}  } var obj=new fun1 (); Obj.eat ()

2.json method
A. When declaring, add
Var obj={property Name: Attribute value, property Name 2: Property value 2, property name 3: Property value 3, ...};

A. add var obj={name when declaring: "Zhang San", Say:function  () {   alert ("Speak");}};                alert (obj.name);        Obj.say ();

B. Declarations added later

B. add var obj={};obj.name= "Zhang San" after the declaration, obj.sex= "man"; obj.play=function  () {  alert ("I will play");} alert (obj.sex); Obj.play ();


Iv. accessing the properties and methods of an object:
The reference value. Properties
The reference value. Attribute ();


V. How to destroy objects
JavaScript's own garbage collection mechanism is to release memory (destroy) when the object is not referenced;
  Object =null;

Vi. How to delete an object's properties

Delete





JavaScript Learning notes-Object basics

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.