JavaScript-Object Oriented

Source: Internet
Author: User

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
  1. Creates an object in memory.
  2. Let this point to this new object.
  3. Execute constructor: Adds properties and methods to this new object.
  4. Returns the new object.

JavaScript-Object Oriented

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.