对象:
- 全局对象:
- 字符串对象(String)
- 日期对象(Date)
- 数组对象(Array)
- 逻辑对象(Boolean)
- 算术对象(Math)
- 正则对象(RegExp)
Date对象:
- toLocaleString() 根据本地时间格式,把Date对象转换为字符串
- toLocaleTimeString() 根据本地时间格式,把Date对象的时间部分转换为字符串
- toLocaleDateString() 根据本地时间格式,把Date对象的日期部分转换为字符串
- 自定义对象:
- 第一种方式
通过关键字 function 定义一个对象。
对象就是有属性和方式的组成。
this.属性名去定义一个属性
this.函数名 = function(){}定义一个函数
如果需要构造器。直接在function中,添加需要的参数名即可
示例:
用function 没有构造
function person (){
this.name
this.age
this.setName = function(name){
this.name = name
}
this.getName = function(){
return this.name
}
}
var p1 = new person();
p1.setName("zdc")
alert(p1.getName)
构造:
function person (name,age){
this.name
this.age
this.setName = function(name){
this.name = name
}
this.getName = function(){
return this.name
}
}
var p2 = new person("adc",22);
alert(p2.getName)
- 第二种方式:
在js中,{}表示一个对象 – 要认识
属性的写法:
属性:值
多个属性需要使用逗号分开,
对象中的函数的定义方式:函数名:function(){函数体}
多个函数需要使用逗号分割,
并且函数和属性也需要使用逗号分开。
最后一个函数或者属性,不需要书写逗号
注意:对象在使用的时候,不需要new,直接赋值给其他变量,或者直接使用
示例:
var person = {
name:"zdc"
age:20
getName:function(){
return this.name;
}
setName: function(name){
this.name = name;
}
}
alert(peroson.getName)
preson.setName("xrn")
alert(person.getName)