The this,new,delete,call,apply of JavaScript

Source: Internet
Author: User

1.this

In general, in JavaScript, this points to the current object when the function executes .

2.new

In JavaScript, after using the new keyword, it means doing the following four things:

    • Creates a new object, the type of which is Object ;
    • Set the internal, accessibility, and [[prototype]] properties of this new object to be set in the constructor (referred to as the constructor pointed to by Prototype.construtor);
    • Executes the constructor when the This keyword is mentioned, using the properties of the newly created object;
    • Returns the newly created object (unless ' No prototype ' is returned in the constructor method).

3.delete

The delete operator is often used to delete an object's properties, rather than a generic variable or function.

4.call

Apply to: Function Object
Invokes one method of an object, replacing the current object with another object.
Call ([thisobj[,arg1[, arg2[, [,. ArgN]]])
Parameters:
Thisobj
Options are available. The object that will be used as the current object.
Arg1, Arg2,, ArgN
Options are available. The method parameter sequence will be passed.
Description
The call method can be used to invoke a method in place of another object. The call method can change the object context of a function from the initial context to a new object specified by Thisobj. If the Thisobj parameter is not provided, then the Global object is used as the thisobj.

5.apply

Call, Apply is a method of Function.prototype, which is implemented internally by the JavaScript engine because it belongs to Function.prototype, so each function object instance, that is, each method has a call , apply property. As a property of the method, the use of them is of course a method. These two methods are easy to confuse, because they work the same way, but only in different ways.

Function.apply (Obj,args) method can receive two parameters
OBJ: This object will replace the This object in the function class
Args: This is an array, which is passed as a parameter to function (args-->arguments)

6. Run and debug the JS code to help understand the above concepts.

7. Use call to implement class inheritance. The code is called "Base.call (This)" in the constructor of Class B, and B can use the methods and properties in Base.

        function B () {            base.call (this);            this.name = ' B ';            This.age = 2;        }                var B = new B ();        B.show ();

JavaScript this,new,delete,call,apply (RPM)

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.