Javascript学习和整理札记--”类“和对象Part(5)

--->如何生成一个对象?

对象?过河不?No

对象,是一个名称与值配对的集合,名称与值的配对称之为属性,所以,也可以说对象是属性的集合。

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

        对象的字面量表示形式,对象={属性名:属性值,属性名:属性值}

        属性名可以是标识符,数值,字符串;属性值可以是任意数据格式,对象或函数均可以的;

        标识符:name

        数值:1

        字符串:'color' 、"color"

var temp = {name:"2tong",
               2:"tong",
          "color":"orange",
      print_hello:function(num){console.log("hello,"+num+"tong");}, //匿名函数,调用时temp.print_hello(num)或temp[print_hello](num)即可
             info:{"age":23,"name":"tong"}}

---->new Object() 形式

       通过new的形式,创建类Object的一个实例化对象

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

--->如何使用对象的函数?

---->构造函数的调用

要实例化类为一个对象的时候,需要调用构造函数。例如,调用JavaScript的内置对象Object的构造函数:

var temp = new Object();

---->对象函数属性的调用

也就是方法调用。

var temp = {name:"2tong",
               2:"tong",
          "color":"orange",
      print_hello:function(num){console.log("hello,"+num+"tong");}, //匿名函数,调用时temp.print_hello(num)或temp[print_hello](num)即可
             info:{"age":23,"name":"tong"}

调用其中的print_hello:

//不支持动态访问
temp.print_hello(2);
//支持动态访问
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!")};

调用其中的print_hello:

//不支持动态访问
temp.print_hello();
//支持动态访问
temp["print_hello"]();


猜你喜欢

转载自blog.csdn.net/orange_612/article/details/80051189