【发布时间】:2017-05-29 02:44:23
【问题描述】:
我在 JavaScript 中使用模块化模式。我想知道我们是否可以防止公共模块被覆盖。 比如下面的代码中function1、function2、function3和function4可以在外面访问但是我不想覆盖。如果这些函数被覆盖,那么我希望编译器生成错误消息
"use strict";
var $ = (function(){
return{
function1 : function(){
alert("this is Function1");
},
function2 : function(){
alert("this is Function2");
},
function3 : function(){
alert("this is Function3");
},
function4 : function(){
alert("this is Function4");
}
};
}());
$.function1(); //will alert - this is Function1
$.function2(); //will alert - this is Function2
/*
I don't want to do this, If I do, then I want the compiler to generate an
error message
*/
$.function3=function(){
alert('function 3 is overridden');
};
$.function3(); //will alert - function 3 is overridden
【问题讨论】:
-
在 SO 上对此问题进行更深入的讨论:stackoverflow.com/questions/366047/…
标签: javascript