JavaScript Object-Oriented Programming Tutorial _ javascript skills

Source: Internet
Author: User
This article mainly introduces the relevant information of the JavaScript Object-Oriented Programming Tutorial. If you need it, you can refer to the definition of objects in JavaScript as a set of unordered attributes, its Attributes can contain basic values, objects, or functions. You can think of an object as a hash, that is, a group of key: value pairs. The values can be data or functions. Each object is created based on a reference type.

Understanding object

There are two ways to create objects in the previous blog: one is to create an object instance, and the other is to use the object literal method:

var person = new Object();person.sex = man;person.name = bluceperson.age = 58;person.sayHi() = function(){console.log('Hello World!');}

However, the following method is used.

var person = {sex:man,name:'bluce',age:'58',sayHi:function(){console.log('Hello World!');}}

Create object

The Object constructor and Object literal method can both create a single Object, but there are obvious disadvantages: using the same interface to create many objects will produce a lot of repeated code. Common Object creation methods include factory mode, constructor model, and prototype mode.

Here I have my own question: a single js file compiled using AMD specifications can be regarded as a module or a "class ", it is a bit confusing with the "class" concept in JavaScript. I hope you can differentiate the application scenarios in the future.

The small Editor of the JavaScript Object-Oriented Programming Tutorial will introduce you here, I hope to help you!

The following describes the JavaScript object-oriented design-factory model.

The factory mode is a well-known design pattern in the software engineering field. This pattern abstracts the process of creating a specific object and can use functions to encapsulate the details of creating objects with a specific interface.

Previously, this design pattern was used in Java DAO, which is easy to understand.

Function createPerson (name, age, sex) {var obj = new Object (); obj. name = name; obj. age = age; obj. sex = sex; obj. sayHi () = function () {console. log (this. name) ;}; // do not omit the quotation marks. return obj;} var person1 = createPerson ("bluce", 58, "man "); var person2 = createPerson ("john", 68, "man ");

You can use this function to create a Person object containing necessary information based on the received parameters. This function can be called countless times. Each time, an object containing three attributes and a method is returned. The factory mode solves the problem of creating multiple similar objects, but does not solve the problem of Object Recognition (how to know the type of an object)

Related Article

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.