Kill lui lei Dog---javascript no:21 custom Class 1

Source: Internet
Author: User

1. Quick Start

Example 1: Defining a person's information through native JS and object-oriented JS

<!DOCTYPE HTML><HTML><Head><MetaCharSet= ' utf-8′><title></title></Head><Body><Script>//define a person's information by JS codevarname=' Lisi'var age = ' 30′;var marry = False;alert (name+ '-' +age+ '-' +marry ');//define a person's information function () by object-oriented () {}// Instantiate the person class var p1 = new Person ();//Assign a value for object P1 Name\age\marry Property P1.name = ' Zhangfei';p 1.age=' -;p 1.marry= false; alert (P1.name+'-'+P1.age+'-'+marry);</Script></Body></HTML>

2. Creation of custom Classes

In JavaScript code, there is no class keyword to define classes, only function, when we define a function through the functions keyword, such as the person function, then the system will consider the function is the same name Class (person Class) constructor, So this is the basic syntax for defining classes in JavaScript code.

Class Person {

function person () {}//constructor for the person class with the same name

}

3. Custom class Execution Process

1) Open up memory space

2) Execute constructor (constructor)

Example code:

<!DOCTYPE HTML><HTML><Head><MetaCharSet= ' utf-8′><title></title></Head><Body><Script>//defining a custom classfunctionPerson () {alert (' Hello ');}varP1= NewPerson ();</Script></Body></HTML>

The above code verifies that when we execute the 14th line of code, the system automatically pops up Hello, so we can prove that the person function is the constructor (constructor) under the person class with the same name

4, about the use of attributes

Question: Why do we use classes to create an object?

A: We create objects to hold more data, and in JavaScript code we can save the data through attributes.

Defines the basic syntax for a property:

Object. Property = property Value

Basic syntax for using attributes:

Object. Properties

Or

Object [' Properties ']

Example code:

<!DOCTYPE HTML><HTML><Head><MetaCharSet= ' utf-8′><title></title></Head><Body><Script>//1. Defining ClassesfunctionPerson () {}//2. Instantiate the person class and create an objectvarP1= NewPerson ();//3. Binding name.email.address Three properties for P1P1.name=' Lisi';p 1.email = ' [email protected]';p 1[' address ']=' Guangzhou, Guangdong province';//4. name.email.address attributes such as alert (p1.name); alert (p1.email); alert (p1.address);</script></body> 

Question: Do we know that there is no class keyword in JavaScript, only function, when we define a function by function, is it referenced in a functional way, or is it referenced in an object-oriented way?

A: In JavaScript, the function is called in object-oriented form, because all properties and methods defined in the global scope are added to the Window object as attributes.

In JavaScript, everything is an object.

var str= ' Hello '; String

var num=10; Number

var marry=true; Boolean

5. Attribute type
    • Digital
    • String
    • Boolean value
    • Object (document)

The object name. Properties

Object name [' property ']

6. Three major keywords

typeof (operator, which is not a function): Determines the data type of the current variable

Instanceof: Determines whether the current object is an instance of a class

Constructor: Returns the constructor or constructor of the current object (returns the constructor pointed to by the prototype object)

How to use:

Alert (typeof p);

Alert (P instanceof person);

alert (p.constructor);

Example code:

<!DOCTYPE HTML><HTML><Head><MetaCharSet= ' utf-8′><title></title></Head><Body><Script>//1. Define a custom classfunctionPerson () {//constructors for the person class with the same name}//Create an ObjectvarP1= NewPerson ();//determine the data type of a P1 objectAlert (typeofp1);//determine if the P1 object is an instance of the person classAlert (P1instanceofPerson );//returns the constructor for the P1 objectalert (p1.constructor);</Script></Body></HTML>

Kill lui lei Dog---javascript no:21 custom Class 1

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.