javascript constructor functions


javascript通过构造函数constructor functions来emulate对象类, 创建对象实例。

对象存储数据或函数等,称为 encapsulated 封装。他们存储在一个对象package里面,称为:namespace命名空间。

model template
模型  模板

定义一个对象template

This is known as abstraction — creating a simple model of a more complex thing, which represents its most important aspects in a way that is easy to work with for our program's purposes.


these new child classes  also known as  subclasses,  can be made to  inherit the data and code features of their parent class,
so you can reuse functionality common to all the object types rather than having to duplicate it.  Where functionality differs between classes, you can define specialized features directly on them as needed.


Note: The fancy word for the ability of multiple object types to implement the same functionality is polymorphism. Just in case you were wondering.

object class / object instances


特别的函数,called constructor functions 去定义和初始化对象和他们的功能。

constructors provide the means to create as many objects as you need in an effective way, attaching data and functions to them as required。
构造函数提供有效的方式去创建对象和附加数据和功能。


function createNewPerson(name) {
  const obj = {};
  obj.name = name;
  obj.greeting = function() {
    alert('Hi! I\'m ' + obj.name + '.');
  };
  return obj;
}

下面是构造函数:首字母大写, The constructor function is JavaScript's version of a class 。构造函数是js的类的一种。js的一种类。

function Person(name) {
  this.name = name;
  this.greeting = function() {
    alert('Hi! I\'m ' + this.name + '.');
  };
}


let person1 = new Person('Bob');
let person2 = new Person('Sarah');

person1.name
person1.greeting()
person2.name
person2.greeting()


keyword:  new   
create a new object instance, followed by the function name.

{
  name: 'Bob',
  greeting: function() {
    alert('Hi! I\'m ' + this.name + '.');
  }
}

{
  name: 'Sarah',
  greeting: function() {
    alert('Hi! I\'m ' + this.name + '.');
  }
}

When we are calling our constructor function, we are defining greeting()  every time, which isn't ideal.
To avoid this, we can define functions on prototype instead, which we will look at later.

declaring an object literal  和使用构造函数,创建对象实例

The Object() constructor

let person1 = new Ojbect()

person1.name = 'Chris';
person1['age'] = 38;
person1.greeting = function() {
  alert('Hi! I\'m ' + this.name + '.');
};
you can add properties and methods to this object using dot or bracket notation as desired;

let person1 = new Object({
  name: 'Chris',
  age: 38,
  greeting: function() {
    alert('Hi! I\'m ' + this.name + '.');
  }
});

With your finished exercise from the previous sections loaded in the browser, try this in your JavaScript console:
let person2 = Object.create(person1);

Now try these:
  1. person2.name;
    person2.greeting();

You'll see that person2 has been created based on person1 as its prototype —it has the same properties and method available to it.

One limitation of create() is that IE8 does not support it. So constructors may be more effective if you want to support older browsers.


阅读量: 631
发布于:
修改于: