JS高程——第5 章引 用 类 型

2020-03-30  本文已影响0人  benbensheng

1.创建 Object 实例

var person=new Object()
person.name="ben";
person.age=12;
var person2={};
person2.name="ben";
person2.age=12;
var person1={
    name:"ben",
    age:12
}
var Person={
    showName:function(){
        return "ben";
    },
    showAge:function(){
        return 12;
    }
}
console.log(Person.showAge()); //12
console.log(Person.showName()); //"ben"

*字面量作为函数的参数

function printName(args){
    console.log(args.name);
    console.log(args.age);
}
printName({name:"ben",age:12});  //ben 12

2.访问对象的属性

var propertyName = "name";
alert(person[propertyName]); //"Nicholas"
或者alert(person["name"]); //"Nicholas"
alert(person.name); //"Nicholas"

3.Array 类型

var colors = new Array();
var colors = new Array("ben","zhou");
var colors = new Array(30);
var colors = ["red", "blue", "green"]; 
var color5;
var color4=["red","blue","white"];
console.log(Array.isArray(color4));  //true
console.log(Array.isArray(color5));   //false
var colors = ["red", "blue", "green"]; // 创建一个包含 3 个字符串的数组
console.log(colors.toString()); // red,blue,green
console.log(colors.valueOf()); // red,blue,green 数组
console.log(colors); // red,blue,green  数组

console.log(colors.join(",")); //red,blue,green
console.log(colors.join("||"));//red||blue||green
image.png
image.png

4.Date对象

image.png
image.png
image.png

5.RegExp 对象

image.png
image.png
上一篇 下一篇

猜你喜欢

热点阅读