var person = { name: "黎明", sex: "男", age: 18, sayHello: function() { console.log("你们好,个人名字是" + this.name + "," + this.sex + ",今年" + this.age) //this 表明当前对象 } } console.log(person.name); //对象的属性 person.sayHello(); //对象的方法
简单的例子:编程
function Car(name, color, num) { this.name = name; this.color = color; this.num = num; this.say = function() { console.log("你们好,我是一辆" + this.name + "车,我是" + this.color + ",有" + this.num + "个轮胎"); } } var lubu = new Car("路虎", "红色", "4"); lubu.say();