JavaScript Basics (objects, functions, and objects)

Source: Internet
Author: User

- , Objects

Properties: Describes an object's information method: Describes the behavior of an object encapsulation: only care about input and output (regardless of how the process is implemented)

ü Categories of objects:

Built-in objects (native objects): Are predefined objects in the JavaScript language (such as String/number).

Host objects: Generally implemented by the browser environment, mainly divided into BOM and DOM.

Custom objects: Objects that are created autonomously by developers.

ü Creation of the object:

    • ? initializer mode

var Object Name = {

Property name : Property value ,

Method name : function () { method body } ---[equivalent to the right half of the literal definition function ]

}

    • constructor function mode

( 1 ) var Object Name       = new Object (); --- new-keyword, which means to create an object

Object Name . Property name = Property Value         ; ---Object-is an ancestor of objects in JavaScript

Object Name . Method Name = function () {};

( 2 ) Custom Constructors

    • Object.create () method

(1) An object must exist in advance Hero1

Eg:var Hero2 = object.create (hero1); The new object created by---has the same properties and methods as the object being passed

(1) Create an empty object

Eg: var object name = object.create (null); Hero4.name = ' zhaomin ';

U Use Object

Method One method two

Console.log ( object name . Property name ); console.log ( object name [' property name ']);

Object Name . Method Name (); object name [' method name '] () ;

[Note: Not applicable to complex naming methods] [Note: For complex naming methods] [More general usage]

Eg:content-type:number---writing errors

'content-type': number; Console.log (Hero.content-type); ---output error

The "-" is resolved to the subtraction operator;  Hero.content-Property acquisition failed; Type-not a variable

Console.log (hero[' content-type '); ---correct

Attached: Window object

* is an object in the BOM that represents the window of the current browser; is also the top-level object in the browser environment (global object)

* Defined global variables and global functions, properties and methods added for Window objects

Eg:var name = ' Zhao min ';

var sayme = function () {

Console.log (' My name is Zhaomin ');

}

Console.log (Window.name);

Window.sayme ();

Properties and methods of the U-action object

2 new objects

Object Name . the new property name = Property Value         ; the name of the object . the new method name = function () {};

2 modifying objects

Object Name . Original Property name = the new property value     ; the name of the object . Original Method Name = function () {};

2 deleting objects

Delete Object Name . Property name Delete Object Name . Method Name

U traversal (enum) object

Only the object name is known, but the property and method name typeof or instanceof are unclear;

    • For...in Cycle:

This method accesses all the enumerable properties of an object and its prototype chain in turn.

Format: for (var A in object name) {A---Represents the object's property name and method name

if (typeof hero[a] = = ' function ') {Distinguishing properties and methods-typeof operator

Hero[a] (); ---must be the method of the object

} else {

Console.log (Hero[a]); ---must be an object's property

}

Result: Gets all the properties or method names of the specified object

    • Object.keys (obj) method:

The obj---represents the traversal of the current object return value: An array containing all the property names and method names of the current object eg:var a = Object.keys (Hero);   Console.log (a); ["Name", "Age", "Sayme"]

    • Object.getownpropertynames (Object) method:

This method is the same as above.

U detects the properties or methods of an object (that is, whether the specified attribute exists in the detection object:)

* Use the IN keyword console.log (' name ' in FN);

Use the hasOwnProperty () method Console.log (Fn.hasownproperty (' name ') for object objects);

Use undefined to judge console.log (fn.name = = = undefined);

Use the IF statement to determine. if (fn.name) {

Console.log (' name attribute exists ');

}

Note: The call object does not exist for the property---undefined

A method that calls an object that does not exist---output error is a function

- , functions, and objects

JavaScript Basics (objects, functions, and objects)

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.