One way to prevent other programmers from inadvertently writing functions with the same name is to put an underscore before the name you want as a private property or method, like this._name = name; This will not avoid all errors, but it will avoid the vast majority of unintended naming conflicts.

 

Scope, function, and Closures

 

Before we can implement encapsulation of private attributes, we need some basic conceptual support. In Javascript, only functions have scopes, that is, variables defined inside a function are not accessible externally. Only variables defined outside the function can be accessed inside the function. Here is an example of an illustration:

function foo(){

    var = 10;

    function bar(){

    a *= 2;

}

bar();

return a;

}

In this example, a is defined in function foo, but a is accessible in function bar because bar is defined in function foo. When bar is executed, it can double the value of a. This is bar executed inside Foo. What if bar was executed outside foo?

function foo(){

    var a = 10;

    function bar(){

        a *= 2;

        return a;

}

return bar;

}

 

var baz = foo();

baz(); // return 20;

baz(); // return 40;

baz(); // return 80;

 

In the above example, the function returns a reference to bar and passes this reference to Baz, who still has access to variable A at execution time. Why is that? Because Javascript supports semantically scoped functions are executed in the domain in which they are defined, not in the domain in which they are executed. In this example bar is defined inside foo, so when it is executed outside of foo, it still has access to the internal variable A.

This is an example of a Closure. Only functions returned in Foo can access variables in foo. The most common way to create a Closure is to return a Nested Function.

 

Create a private method using Closure

With this foundation, we can implement private methods as follows:

            var NewHouse = function(hid,hname,address){

                var _hid,_hname,_address;

                

                function isValid(hid){

if(hid ! = null && hid ! = undefined & hid ! {= “”)

                        return true;

                    }else{

                        return false;

                    }

                }

                

                this.getHid = function(){

                    return _hid;

                }

                

                this.setHid = function(hid){

if(! isValid(hid)){

                        _hid = hid;

                    }

                }

                

                this.getHname = function(){

                    return _hname;

                }

                

                this.setHname = function(hname){

_hname = hname | | “no”.

                }

                

                this.getAddress = function(){

                    return _address;

                }

                

                this.setAddress = function(address){

_address = address | | “no”.

                }

                

                this.setHid(hid);

                this.setHname(hname);

                this.setAddress(address);

            }

            

            NewHouse.prototype.ShowHouse = function(){

Document.getelementbyid (“container”).innerhtml += “this. Hname + “

“;

            }

 

As you can see, in the previous example we used the this keyword to refer to the property. Here we use var to define the property, so the property cannot be accessed outside the object. For some common methods, we can still use Prototype to define them.

The drawback of this method is that every time we build an object, the internal method pairs need to be initialized, which consumes memory. The more objects we instance, the more memory we consume. So this approach is only used where we really need to privatize properties and methods, but elsewhere we can still do it in a completely exposed way.