JavaScript design mode new Create Safe mode for objects

Source: Internet
Author: User
Tags closure
for us beginners, when using JS to create objects, do not adapt to the closure of the writing, often easy to forget to use the new keyword and make mistakes.

A closure is a function that has access to a variable in another function scope, that is, creating another function inside a function. We use this closure as the constructor for creating an object so that he can access the variable in the scope of the class function, such as the booknum variable, when it is both a closure and a function of the instantiated object, and the variable is called a static private variable, and checkbook () is called a static private method. Of course the closure also has its own private variables and private methods such as Price,checkid ().

Using closure to implement
Var book= (function () {
//static private variable
var booknum = 0;
Create Private method
function Checkbook (name) {}
//Create class
function book (newid,newname,newprice) {
    //private variable
    var Name,price;
    Private method
    function Checkid (id) {}
    //Privileged Method
    This.getname=function () {};
    This.getprice=function () {};
    This.setname=function () {};
    This.setprice=function () {};
    public attribute
    This.id=newid;
    Public method
    This.copy=function () {};
    booknum++
    if (booknum>100) {
        throw new Error (' No more books. ')
    This.setname (name);
    This.setprice (price);
}
    Building
    a prototype _book.prototype={
        //Static public property
        Isjsbook:false,
        //Static Common method
        Display:function () {}
    };
    Returns class return
    _book;
}) ();

New Create Class

Books
var book = function (title,time,type) {
    this.title = title;
    This.time = time;
    This.type = type;
}
Instantiate a book
var = books (' JavaScript ', ' 2014 ', ' JS ');

Results:
console.log (book),//undefined
Console.log (window.title),//javascript
Console.log (window.time) ; 2014
Console.log (window.type);//js

The New keyword action can be seen as a constant assignment of this to the current object, but the example does not use new, so the function is executed directly, and the function is executed in the global scope, so this point in the global scope is the current object, which is naturally a global variable, Global variable is window in page

Therefore, you can use the following security mode:

//Books security class var book = function (Title,time,type) {//Determine whether this is the current object in the execution (if the description is created with new) if (this
        instanceof book) {this.title = title;
        This.time = time;
        This.type = type;
    Otherwise recreate this object}else{return new book (Title,time,type);


} var book= book (' JavaScript ', ' 2014 ', ' JS '); Output Result: Console.log (book); Book Console.log (Book.title); JavaScript console.log (book.time); 2014 Console.log (Book.type); JS Console.log (Window.title); Undefined Console.log (window.time); Undefined Console.log (window.type); Undefined 
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.