jQuery 有条件的初始化函数
jQuery init function with condition
我是脚本方面的新手,再次需要您的帮助来编写更好的代码。我正在使用 init 函数,它是所有函数的集合,但现在似乎只有少数函数在某些条件为真时我想执行。
示例脚本
$(document).ready(function() {
var app = {
init: function(){
// Variable to check condition
var abc = 0;
// Default functions
this.Function1();
this.Function2();
this.Function3(); // abc variable will change to 1 if condition is true
// Conditional functions
if ( abc === 1 ) {
this.ConditionFunction1();
this.ConditionFunction2();
},
Function1: function(){ // some Code },
Function2: function(){ // some Code },
Function3: function(){
if(true) { abc == 1; }
},
ConditionFunction1: function(){ // some Code },
ConditionFunction2: function(){ // some Code }
}
app.init();
});
通过重写和定位你的代码,你可以这样写:
$(document).ready(function () {
var app = {
init: function () {
// Variable to check condition
var abc = 0;
var Function1 = function () { // some Code
console.log("Function1")
}
Function2 = function () { // some Code
console.log("Function2")
}
Function3 = function () {
if (true) {
console.log("Function3")
abc == 1;
}
}
ConditionFunction1 = function () { // some Code
console.log("ConditionFunction1")
}
ConditionFunction2 = function () { // some Code
console.log("ConditionFunction2")
}
// Default functions
Function1();
Function2();
Function3(); // abc variable will change to 1 if condition is true
// Conditional functions
if (abc === 1) {
ConditionFunction1();
ConditionFunction2();
}
}
}
app.init();
});
我是脚本方面的新手,再次需要您的帮助来编写更好的代码。我正在使用 init 函数,它是所有函数的集合,但现在似乎只有少数函数在某些条件为真时我想执行。
示例脚本
$(document).ready(function() {
var app = {
init: function(){
// Variable to check condition
var abc = 0;
// Default functions
this.Function1();
this.Function2();
this.Function3(); // abc variable will change to 1 if condition is true
// Conditional functions
if ( abc === 1 ) {
this.ConditionFunction1();
this.ConditionFunction2();
},
Function1: function(){ // some Code },
Function2: function(){ // some Code },
Function3: function(){
if(true) { abc == 1; }
},
ConditionFunction1: function(){ // some Code },
ConditionFunction2: function(){ // some Code }
}
app.init();
});
通过重写和定位你的代码,你可以这样写:
$(document).ready(function () {
var app = {
init: function () {
// Variable to check condition
var abc = 0;
var Function1 = function () { // some Code
console.log("Function1")
}
Function2 = function () { // some Code
console.log("Function2")
}
Function3 = function () {
if (true) {
console.log("Function3")
abc == 1;
}
}
ConditionFunction1 = function () { // some Code
console.log("ConditionFunction1")
}
ConditionFunction2 = function () { // some Code
console.log("ConditionFunction2")
}
// Default functions
Function1();
Function2();
Function3(); // abc variable will change to 1 if condition is true
// Conditional functions
if (abc === 1) {
ConditionFunction1();
ConditionFunction2();
}
}
}
app.init();
});