JavaScript对象

https://www.runoob.com/js/js-objects.html

JavaScript中所有事物都是对象:字符串、数值、数组、函数...

对象只是一种特殊的数据。对象拥有属性和方法。

var message="Hello World";
var x=message.length;
var x=message.toUpperCase();

创建JavaScript对象:

(1)使用Object定义并创建对象的实例:这种是不是无法添加函数?怎么添加?

var person=new Object();
person.firstName="John";

var o = new Object(true);    //创建布尔对象

person={firstname:"John",lastname:"Doe",age:50,eyecolor:"blue"}

(2)使用函数来定义对象,然后创建新的对象实例。

function person(firstname,lastname,age,eyecolor){
    this.firstname=firstname;
    this.lastname=lastname;
    this.age=age;
    this.eyecolor=eyecolor;
// function changeName(name) {
//     this.lastname = name;
// }
}
person.prototype.changeName=function(name){
        this.lastname=name;
};
myMother=new person("Sally","Rally",48,"green");

myMother.changeName("Doe");
document.write(myMother.lastname);

 

posted @ 2021-11-06 21:57  kuaqi  阅读(26)  评论(0编辑  收藏  举报