Object oriented language ( Such as Java) There is the concept of class in , Through classes, you can create any number of objects with the same properties and methods . however ,JavaScript There is no concept of a class , So its objects are also different from those in class based languages .

To understand object orientation , First of all, we need to understand Javascript Objects and prototypes in . This article mainly studies Javascript Objects in the .

One About objects

An object is actually a reference type . The value of an object is an instance of the reference type . stay JavaScript A reference type in is a data structure , Organizing data and functions together . It is also often called a class , but JavaScript There is no concept of class in . although JavaScript It is an object-oriented language , But it doesn't have the basic structure of class and interface supported by traditional object-oriented language .

Two Object creation and common operations

1. Use new Operator

<script>
     var user = new Object();        // Use new Operator to create an object
     user.name = ' By the triangle lake ';        // Add properties to the object
     user.age = 22;
     user.address = ' Wuhan, Hubei ';
     alert(user.name + " " +user.age);// return ' By the triangle lake Wuhan, Hubei '
 </script>

explain : In the way above new Keywords can be omitted , namely var user = new Object(); Equivalent to var user = Object();

2.JSON Law creation

About JSON Some of the knowledge can be searched on Baidu .

ordinary JSON object : {name:' By the triangle lake ',age:22,address:' Wuhan, Hubei '}

// Use JSON Law creation
    /*
        ordinary JSON object :{name:' By the triangle lake ',age:22,address:' Wuhan, Hubei '}
    */
    var user = {
        name:' By the triangle lake ',
        age:22,
        address:' Wuhan, Hubei '   
    };
    alert(user.name + " " +user.age);// return ' By the triangle lake Wuhan, Hubei '

3. The traditional assignment method

// Traditional assignment
    var user = {};
    user.name = ' By the triangle lake ';        // Add properties to the object
    user.age = 22;
    user.address = ' Wuhan, Hubei ';
    alert(user.name + " " +user.age);// return ' By the triangle lake Wuhan, Hubei '

4. Call to property

There are two ways to call object properties :

Take the example above , We've already used one of these ways , That is to say '.' Operator , The method to invoke is as follows :

alert(user.name + " " +user.age);// return ' By the triangle lake Wuhan, Hubei '

Another way :
alert(user['name'] + " " +user['age']);// return ' By the triangle lake Wuhan, Hubei

5. Add methods to objects

Adding a method to an object is similar to adding a property to a variable . The specific code is as follows :

var user = {
        name:' By the triangle lake ',        // Add properties to the object
        age:22,
        address:' Wuhan, Hubei ',
        showInfo:function(){// Add a method
            alert(this.name+" "+this.age+" "+this.address);// return ' By the triangle lake 22 Wuhan, Hubei '    
        },
        showHello:showHello// Add methods outside the object to the object
    };
    function showHello(){
        alert("Hello!");    
    }
    user.showInfo();// Calling method
    user.showHello();

6. Deletes an object's properties

Delete the properties of an object with delete The operator . Format :delete Object name . The attribute name

var user = {
        name:' By the triangle lake ',        // Add properties to the object
        age:22,
        address:' Wuhan, Hubei '
    };
    alert(user.name);// return ‘ By the triangle lake ’
    delete user.name;// Delete user Of name attribute
    alert(user.name);// return ‘undefined’

3、 ... and summary

Here is a brief introduction Javascript Object creation, object method addition and object attribute deletion in . The object is javascript Object oriented is an important one , It's also a relatively basic part , Only when we have a clear understanding of some common operations of the object can we carry out object-oriented development .

Javascript Objects and prototypes in ( One )( Reprint ) More articles about

  1. Javascript Objects and prototypes in ( 3、 ... and )( Reprint )

    stay Javascript Objects and prototypes in ( Two ) We mentioned in , Objects created with constructors , Each object is independent of the other , This will reduce the utilization of system resources , To solve this problem , We're going to use the prototype objects mentioned below . One Prototype object Prototype ...

  2. Javascript Objects and prototypes in (3)

    stay Javascript Objects and prototypes in ( Two ) We mentioned in , Objects created with constructors , Each object is independent of the other , This will reduce the utilization of system resources , To solve this problem , We're going to use the prototype objects mentioned below . One Prototype object Prototype ...

  3. javascript Objects in the , Prototype , Prototype chains and object orientation

    One .javascript Properties in . Method 1. First , About javascript The function in /“ Method ”, Here are two points : 1) If the object property being accessed is a function , It's easy for some developers to think that this function belongs to this object , So the “ Attribute access ” be called ...

  4. JavaScript Objects and prototypes in — You don't know JavaScript Volume one reading notes ( Four )

    One . object Objects can be defined in two forms : Statement ( written words ) Form and construction form . namely : var myObj = { key: value // ... }; or : var myObj = new Object(); m ...

  5. Javascript Objects and prototypes in ( Two )( Reprint )

    The last one mentioned JavaScript Some basic operations of creating objects in , Next, let's continue to discuss . One Factory mode We know , To create an object, we can use the following code : var user = new Object(); // ...

  6. Javascript Objects and prototypes in

    One Prototype object A prototype object is actually an instance object of a constructor , There is no essential difference from ordinary instance objects . Can contain shared properties or methods for all instances of a particular type . such , If we need to modify properties or methods in all instances , It's just one change ...

  7. JavaScript All kinds of methods to determine the type of object in the

    We know ,JavaScript The operators that detect object types in are :typeof.instanceof, There are also objects constructor attribute : 1) typeof Operator typeof It's a unary operator , The return result is a ...

  8. Javascript The judgment object in is empty

    Found a clever implementation :  You need to check an object (Object) Is it empty , That is, it does not contain any elements .Javascript The object in is a dictionary , It contains a series of key value pairs (Key Value Pair). Check an object is ...

  9. JavaScript Built in objects and prototype inheritance

    ( One )   understand JavaScript Class definition 1> About understanding built-in objects console.log(Date.prototype.__proto__===Object.prototype    //tru ...

Random recommendation

  1. BZOJ1010 [HNOI2008] The toy box toy

    The copyright of this article belongs to ljh2000 And blog park , Welcome to reprint , But keep this statement , And give a link to the original , Thank You for Your Cooperation . The author of this article :ljh2000 The author blog :http://www.cnblogs.com/ljh2000-jump/ turn ...

  2. debugging WebService

    1. function WebService The calling program of 2. Run in browser asmx, This step is to make w3wp.exe Appears in the next list 3.“ Tools ” or “ debugging ” menu --> Attach to process (MS Why put the same function in different ...

  3. go log repl

    package main import ( "fmt" "os/exec" "regexp" "strconv" &qu ...

  4. SSD Cloud Hosting&ndash;Linode-Mysql Database installation and configuration

    And then the last topic :SSD Cloud Hosting - Linode Configuration and deployment of , build Java Environmental Science 8.Mysql Database installation and configuration install Check yum Is there any in it mysql: yum list|gre ...

  5. memcached and mongodb stay windows Lower installation

    To install... On a new machine memcached and mongodb service , I've been tossing about all day , Finally put these two services in windows I'm running . memcached Main reference http://www.rootop.org/pages/27 ...

  6. The front end teaches you UI—— Character handling ( One )

    One . preface This article is the first in this series UI The article , It's necessary to state something at the beginning , The main purpose of this series is to learn as a blogger after working in the front end UI A record of , At the same time, in order to let more peers learn something other than programming . So this article will try to ...

  7. C# and NewSQL Better match —— CockroachDB introduction ( May be C# The first round of the whole network )

    Read the directory CockroachDB What is it? The deployment environment actual combat Performance testing Conclusion One .CockroachDB What is it? CockroachDB(https://www.cockroachlabs.com) yes Goog ...

  8. 【HIVE】sql Statement conversion to mapreduce

    1.hive What is it? ? 2.MapReduce Framework implementations SQL What is the principle of basic operation ? 3.Hive How to achieve SQL Lexical and grammatical analysis of ? Connect :http://www.aboutyun.com/thread-20 ...

  9. MSSQL Custom function details

    Custom functions are divided into : Scalar valued functions or table valued functions If RETURNS Clause specifies a scalar data type , Then the function is a scalar valued function . You can use more than one Transact-SQL Statement defines a scalar valued function . If RETURNS Clause assignment ...

  10. Web Automated testing framework sweetest Introduce

    Project open source : https://github.com/tonglei100/sweetest Reprint of the article :https://segmentfault.com/a/1190000011612061 Introduce swe ...