我今天看到这样的一个js代码
class5 = (function() {
// private static fields
var s_first = 1;
var s_second = 2;
// private static methods
function s_method1() {
s_first++;
}
var s_second = 2;
function constructor() {
// private fields
var m_first = 1;
var m_second = 2;
// private methods
function method1() {
alert(m_first);
}
var method2 = function() {
alert(m_second);
}
// public fields
this.first = "first";
this.second = ['s','e','c','o','n','d'];
// public methods
this.method1 = function() {
s_second--;
}
this.method2 = function() {
alert(this.second);
}
// constructor
{
s_method1();
this.method1();
}
}
// public static methods
constructor.method1 = function() {
s_first++;
alert(s_first);
}
constructor.method2 = function() {
alert(s_second);
}
return constructor;
})(); 在function 外面加个刮号是什么意思。后面那个()是干什么用的。请高手指点下。
class5 = (function() {
// private static fields
var s_first = 1;
var s_second = 2;
// private static methods
function s_method1() {
s_first++;
}
var s_second = 2;
function constructor() {
// private fields
var m_first = 1;
var m_second = 2;
// private methods
function method1() {
alert(m_first);
}
var method2 = function() {
alert(m_second);
}
// public fields
this.first = "first";
this.second = ['s','e','c','o','n','d'];
// public methods
this.method1 = function() {
s_second--;
}
this.method2 = function() {
alert(this.second);
}
// constructor
{
s_method1();
this.method1();
}
}
// public static methods
constructor.method1 = function() {
s_first++;
alert(s_first);
}
constructor.method2 = function() {
alert(s_second);
}
return constructor;
})(); 在function 外面加个刮号是什么意思。后面那个()是干什么用的。请高手指点下。
val = ( function(){ return 5 } )()
结果就是val=5, 相当于:function fun()
{
return 5
}val = fun()
function() {
// private static fields
var s_first = 1;
var s_second = 2;
// private static methods
function s_method1() {
s_first++;
}
var s_second = 2;
function constructor() {
// private fields
var m_first = 1;
var m_second = 2;
// private methods
function method1() {
alert(m_first);
}
var method2 = function() {
alert(m_second);
}
// public fields
this.first = "first";
this.second = ['s','e','c','o','n','d'];
// public methods
this.method1 = function() {
s_second--;
}
this.method2 = function() {
alert(this.second);
}
// constructor
{
s_method1();
this.method1();
}
}
// public static methods
constructor.method1 = function() {
s_first++;
alert(s_first);
}
constructor.method2 = function() {
alert(s_second);
}
return constructor;
}
)(); 这样是不是容易看懂些,注意层次结构
()跟在一个js 的 function后面,就相当于运算符。如一楼
val = ( function(){ return 5 } )() //运算
结果就是val=5, 相当于: function fun()
{
return 5
} val = fun()//运算
style.width = style.height = "100%"; style.backgroundColor = "#fff"; style.filter = "alpha(opacity:0)";
})(this.Drag.appendChild(document.createElement("div")).style)
那这段代码是什么意思。还是不理解,比较笨。不好意思
类似这个样
var cms=(function(str){alert(str);})('我被声明了,我也被执行了');