阅读:4334回复:1
给JS对象添加属性和方法方式一:在定义对象时,直接添加属性和方法 直接赋值即可: var Arr = {} Arr.name = "小明"; Arr.age = "12"; 或 function Person(name,age) { this.name = name; this.age = age; this.say = function() { alert(name + ':::' + age); } } var person = new Person('张三', 24); person.say(); 方式二:通过"对象.属性名"的方式添加 function Person() {} var person = new Person(); person.name = '张三'; person.say = function() {alert(this.name)}; person.say(); 方式三:通过prototype(原型)属性添加 function Person() {} var person = new Person(); Person.prototype.name = '张三'; Person.prototype.say = function() {alert(this.name)}; person.say(); |
|
沙发#
发布于:2021-02-06 14:40
|
|