JavaScript learning-object-oriented

Source: Internet
Author: User
JavaScript objects are all objects in JavaScript. objects are collections of variables and functions, or objects are only special data types with attributes and methods. In other object-oriented languages, objects are instantiated by classes, Jav


What is a JavaScript Object?


All things in JavaScript are objects. objects are collections of variables and functions, or objects are only special data types with attributes and methods. In other object-oriented languages, objects are instantiated by classes. Other object-oriented languages in JavaScript are different because JavaScript is a prototype-based object-oriented language without the concept of classes, all objects are derived from a copy of an existing object.

Create a JavaScript Object


JavaScript contains many objects, including Array, String, Boolean, Number, Mah, Data, RegExp, and Global. In addition, JavaScript allows custom objects.

Through JavaScript, we can define and create our own objects. There are two different ways to create a new object:

1. Define and create an Object instance to use the Object;

2. Use the constructor Function to define the object and then create a new object instance.


Method 1 Object

Object method to create an Object and its instance:

varperson=new Object();person.firstname="Bill";person.lastname="Gates";person.age=56;person.eyecolor="blue";


Instance:

person={firstname:"John",lastname:"Doe",age:50,eyecolor:"blue"};


Method 2 Fucntion


Use the Fucntion constructor to create an object and its instances.


 function person(firstname,lastname,age,eyecolor){this.firstname=firstname;this.lastname=lastname;this.age=age;this.eyecolor=eyecolor;}


Create an object instance using Constructors

var myFather=newperson("Bill","Gates",56,"blue");


Note: When Using constructors to create objects, there is another method to create objects. However, this method is not as good as the above method in terms of performance and has strict requirements on the Declaration Order, it is not very convenient to use, so it is not recommended to use:

var person=new Function (firstname,this.firstname=firstname;lastname,this.lastname=lastname;age,this.age=age;eyecolor,this.eyecolor=eyecolor;);


Add Member
Add static members


1 Object Method


Adding a static member to an Object only exists in the myObj instance of the Object and does not exist in the constructor.

Var myObj = newObject (); // Add the name attribute myObj. name = 'ld '; // Add the alertName method myObj. alertName = function () {alert (this. name);} // execute alertNamemyObj. alertName ();


2 Function Method


You can add a static member to a Function either in the myObj instance or in the constructor.

Var myObj = newfunction () {// Add the static property myObj. name = 'ld '; // Add the static method myObj. alertName = function () {alert (this. nam) ;}} myObj. alertName ();


The static members added above can only be used in the myObj instance, but cannot be used in other instances. Otherwise, an error is reported. This is the application of the static members.

Add public members


The method that can contain the instantiated object is called a public member. You need to modify the prototype attribute of the function, any existing instance of the object that inherits the object will immediately inherit the unified changes.

FunctionmyConstructor () {}// Add the public attribute myConstructor. prototype. myName = 'ld '; // instantiate var myObj = newmyConstructor (); alert (myObj. myName );


Objects instantiated by myConstructor can use myName, but myConstructor itself cannot, because we add public members to the underlying definition of myConstructor, rather than the myConstructor instance itself.

Add Private Members


Private Members are the variables and functions defined in the constructor. This is relatively simple as long as the attributes and methods in the braces of the constructor are private.

FunctionmyConstructor () {// Add the private property var myName = 'ld 'l // Add the private method varalertName = function () {alert ('ld');} alertName ();


Add privileged members


Unlike private members, privileged members can be publicly accessed and can also access private members. privileged members refer to the methods defined by this in the scope of constructors.

FunctionmyConstructor () {// Private Attribute varname = 'dev'; // privileged method this. alertName = function () {alert (name );}}


Comparison between Members


Both private and privileged members react within the function. They are taken to every instance of the function, which occupies a large amount of memory. Public prototype members are part of the object blueprint, applies to instantiating each instance of the object using the new keyword. Static members only apply to one special instance of the object.

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.