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();