The js object code template that's easy to understand, just don't know how to clone a new object

87 1
<script>
var DED = function() {
 var pvar=3;
 function private_method() {
//do stuff here
 pvar = 5;
 }
 return {
 method_1 : function() {
//do stuff here
 private_method();
 alert(pvar);
 pvar = 6;
 },
 method_2 : function() {
//do stuff here
 alert(pvar);
 }
 };
}();
DED.method_1();
DED.method_2();
</script>
3 Answers

108 4
var a = new DED(),
 b = new DED();
a.method_1();
b.method_1();
90 5

The streamlined code is

var DED = ( function(){ return {} ; }() );

Indicates that it returns an object and isn't a function so that the should be lost.

DED;//是对象并不是函数

To copy an object, you can use the following code

 function object(o) {
    function F() {}
    F.prototype = o;
    return new F();
  }
var DED1 = object(DED);
var DED2 = object(DED);
DED1.method_1();

Of course, you can use a copy attribute.

//至于这种方式 因为JavaScript是引用传递 所以会造成原有的函数也会被修改
var DED1 = DED;
DED1.method_1 = function(){alert("34535");};
DED.method_1();
...