js怎样写get方法和set方法

JavaScript09

js怎样写get方法和set方法,第1张

  var student = new Object()

        student.name = "张三"

        student.age = 18

        student.sex = 'man'

        

        document.write(student.name +"</br>")

        document.write(student.age + "</br>")

        document.write(student.sex)

test对象的这种写法,对IE是不兼容的,在IE下报错

谷歌内核的浏览器是通过测试的。

由于test是对象Object,不是function,所以不能new 对象的形式出现。

除非

var test = function (name, age){

    this.name = name

    this.age = age

}

test.prototype.constructor = test

test.prototype.setAge = function (age) {

    this.age = age

}

test.prototype.setName = function (name) {

    this.name = name

}

test.prototype.getAge = function (){

    return this.age

}

test.prototype.getName = function (){

    return this.name

}

var t = new test()

t.setName("sss")

t.getName()