Javascript learning and finishing notes -- "class" and object Part (5)

---> How to generate an object?

object? Cross the river? No

An object is a collection of pairs of names and values, and the pairings of names and values ​​are called attributes, so it can also be said that an object is a collection of attributes.

---->JSON(JavaScript Object Notation)形式

        Literal representation of object, object={proper_name:property_value, property_name:property_value}

        Attribute names can be identifiers, numbers, or strings; attribute values ​​can be in any data format, such as objects or functions;

        Identifier: name

        Value: 1

        字符串:'color' 、"color"

var temp = {name:"2tong",
               2:"tong",
          "color":"orange",
      print_hello:function(num){console.log("hello,"+num+"tong");}, //Anonymous function, when calling temp.print_hello(num) or temp[print_hello](num)
             info:{"age":23,"name":"tong"}}

---->new Object() form

       Create an instantiated object of class Object in the form of new

var temp = new Object();
temp.name = "2tong";
temp.age = 23;
temp.favor = {'color':"orange",'food':"ice"};
temp.print_hello = function(){console.log("hello,2tong!")};

---> How to use the functions of the object?

----> Invocation of the constructor

To instantiate a class as an object, you need to call the constructor. For example, calling the constructor of JavaScript's built-in object Object:

var temp = new Object();

----> Invocation of object function properties

That is, method calls.

var temp = {name:"2tong",
               2:"tong",
          "color":"orange",
      print_hello:function(num){console.log("hello,"+num+"tong");}, //Anonymous function, when calling temp.print_hello(num) or temp[print_hello](num)
             info:{"age":23,"name":"tong"}

Call print_hello in it:

//Dynamic access is not supported
temp.print_hello(2);
// support dynamic access
temp["print_hello"](2);    
var temp = new Object();
temp.name = "2tong";
temp.age = 23;
temp.favor = {'color':"orange",'food':"ice"};
temp.print_hello = function(){console.log("hello,2tong!")};

Call print_hello in it:

//Dynamic access is not supported
temp.print_hello();
// support dynamic access
temp["print_hello"]();


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324731374&siteId=291194637