JavaScript代码:
// 1、字面式的对象声明:最简单,好理解,推荐使用; var person = { name: "Jack", age: 20, eat: function () { alert("人类都可以吃!"); } }; person.addr = "上海"; // 给对象添加属性; alert("name: " + person.name); alert("age: " + person.age); alert("addr: " + person.addr); person.eat(); // 2、使用模拟类的方式创建对象:由于JS没有类,可以使用function模拟一个类; // 模拟类中方法的声明有三种:(1)、类方法 (2)、原型方法 (3)、对象方法; // 写法1:无参构造函数;又可分为 普通写法 和 原型写法; function Person() { } // 普通写法:先实例化对象,再赋值; var stu = new Person(); // 用 new 关键字去实例化一个Person对象; stu.name = "Tom"; stu.age = 20; stu.addr = "上海"; stu.eat = function () { // (1)、类方法; alert("我叫" + stu.name + ",我今年" + stu.age + "岁了,我住在" + stu.addr); } stu.eat(); // 原型写法:使用关键字 prototype;先赋初值,然后再实例化对象; Person.prototype.name = "Ken"; Person.prototype.age = 33; // prototype:该属性使模拟类可以被添加属性和方法; Person.prototype.addr = "苏州"; Person.prototype.eat = function () { // (2)、原型方法; alert("我叫" + this.name + ",我今年" + this.age + "岁了,我住在" + this.addr); } var teacher = new Person(); teacher.eat(); // 写法2:有参构造函数;先给当前对象赋值,然后再实例化对象,赋初值; function Person(name, age, addr) { this._name = name; // this:当前对象; this._age = age; this._addr = addr; this.eat = function () { // (3)、对象方法; alert("我叫" + this._name + ",我今年" + this._age + "岁了,我住在" + this._addr); } Person.prototype.fly = function () { // (2)、原型方法;写在类里面; alert("人类会飞!"); } } Person.prototype.run = function () { // (2)、原型方法;写在类外面; alert("人类会跑步!"); } var stu = new Person("Lucy", 22, "南京"); // 实例化一个Person对象,并赋初值; stu.eat(); stu.fly(); stu.run(); // 3、JavaScript模拟类的封装; 封装语法:( function () { 类的主体 } () ); (function () { function people(name, age) { // 声明一个类; this._name = name; this._age = age; } people.prototype.sayHello = function () { // 原型方法; alert("People_Say: 我叫" + this._name + ", 我今年" + this._age + "岁了!"); } window.People = people; // 必须将类放到最顶层的 window对象 上才能执行; }()); var people = new People("Tom", "22"); // 实例化一个 People 对象; people.sayHello(); // 4、继承; // 封装一个子类(Student),继承于父类(People): (function () { function Student(name, age) { this._name = name; this._age = age; } Student.prototype = new People(); // 使student类继承于People类; var stuSay = Student.prototype.sayHello; // 继承父类中的方法; //var stuSay = Student.prototype.sayHello(); // 写成这种方式会直接执行父类中的方法; // 子类重写父类的方法; Student.prototype.sayHello = function () { stuSay.call(this); // 重写之后,子类只会调用自己的方法;如果想调用父类的方法,可以使用该方式: alert("Student_Say: 我叫" + this._name + ", 我今年" + this._age + "岁了!"); } window.Student = Student; // 将 Student类 传给最顶层的 window 对象; }()); var stu = new Student("小花", "18"); stu.sayHello(); // 5、空对象:用于承载类中的属性和方法,还可以用于类的继承; function Father(name, age) { // 声明一个Person类; var _this = {}; // 声明一个空对象,用来承载类中的属性和方法; _this._name = name; _this._age = age; _this.sayHello = function () { // 对象方法; alert("Father_say: 我叫" + _this._name + ", 我今年" + _this._age + "岁了!"); } return _this; // 使用空对象承载属性和方法,最后必须 return _this; } var father = new Father("张三", "33"); father.sayHello(); // 声明一个子类: function Son(name, age) { var _this = Father(name, age); // 声明一个空对象,继承父类(Father); _this._name = name; _this._age = age; var sonSay = _this.sayHello; // 继承父类的方法; _this.sayHello = function () { // 子类重写父类的方法; sonSay.call(this); // 调用父类的方法; alert("Son_say: 我叫" + _this._name + ", 我今年" + _this._age + "岁了!"); } return _this; } var son = new Son("小三", "11"); son.sayHello();