Sound constructor function of JavaScript constructor __ function

Source: Internet
Author: User
Tags instance method

A trustworthy object is one that does not have a public property and its methods do not refer to this object. A secure object function follows a pattern similar to a parasitic constructor, but has two different points: one is that the instance method of the newly created object does not reference this, and the constructor is not invoked with the new operator.

When browsing blogs recently, I found that a lot of people seem to understand the sound constructor, and one of the features of this function, and the other constructors mentioned in the JavaScript Advanced program, is that private quantization can be realized by using JS scope.

function Process (NAMEF) {
    var o = new Object ();
    var name = namef;//1
    o.sayname = function () {
        alert (name);//2
    };
    return o;
}

var friend = Process (' Nick ');
Friend.sayname ();  "Nick"
Console.log (friend.name);//UNDEFINED//3
Note: (3 points below) 1. In a secure constructor, a variable cannot be suspended to 2 of the object being returned . UseThis 3 when you are working with custom functions in a secure constructor. Use a secure constructor outside of a function without new.
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.