1-4 how objects are created
property is the characteristic of the object, and method is the behavior of the object.
1. Object literals
var o = { name: ‘zs‘, age: 18, sex: true, sayHi: function () { console.log(this.name); }};
2. Create object with new Object ()
var person = new Object(); person.name = ‘lisi‘; person.age = 35; person.job = ‘actor‘; person.sayHi = function(){ console.log(‘Hello,everyBody‘);}
3. Factory function Creation Object
function createPerson(name, age, job) { var person = new Object(); person.name = name; person.age = age; person.job = job; person.sayHi = function(){ console.log(‘Hello,everyBody‘); } return person;}var p1 = createPerson(‘allen‘, 22, ‘actor‘);
4. Custom constructors
function Person(name,age,job){ this.name = name; this.age = age; this.job = job; this.sayHi = function(){ console.log(‘Hello,everyBody‘); }}var p1 = new Person(‘allen‘, 22, ‘actor‘);
5. Work when the new keyword is executed
- Creates an object in memory.
- Let this point to this new object.
- Execute constructor: Adds properties and methods to this new object.
- Returns the new object.
JavaScript-Object Oriented