我已經採用了一個項目,它定義了幾個對象文字 - 主要是爲了構造代碼。順便說一句,不是一個全職的前端開發人員。問題是它們中的一些是使用jQuery onready事件定義的,有些則在外面。至少可以說這似乎是很有問題的。這會被認爲是不好的設計?我假設如此。我已經包含的代碼片段顯示了一些問題,特別是OBJ2內的from_inside功能:訪問使用jQuery onready定義的對象字面值from
<script>
$(document).ready(function(){
alert('here i am' + obj2.handlers2.background2); // can access the obj2 value as expected
alert('make this up: ' + obj2.handlers2.tell_me2());
var obj={};
obj.handlers={
background: 'blue',
foreground: 'green'
}
});
var obj2={};
obj2.handlers2={
background2: 'here i am jt',
foreground2: 'there you are',
tell_me2: function(){
return "these are things";
}
,
from_inside: function(){
alert('color from inside jQuery: ' + obj.handlers.background); // problem here! could I get a reference to obj here?
}
};
obj2.handlers2.from_inside();
</script>
鑑於我們當前如何擁有它,這將是一個最好的解決方法?看起來好像我可以在上面將jQuery對象的引用傳遞給obj2,但也許可能只是簡單地將jQuery之外的所有對象都準備好,即使可能的話也是如此。
事先THX的任何建議
編輯#1
$(document).ready(function(){
$(document).on('click','#pairing-comment-submit', function(){
arc.event_handler.submit_pairing_comment.call(this);
});
... about 50 more of these
});
arc={};
arc.event_handler={
submit_pairing_comment: function(){
var id=$(this).data('the-id');
....
},
....
}
剛剛做了一個快速重構。讓我知道這是否有幫助。 –