이것이 작동하지 않는 이유를 말씀해 주시겠습니까? 예상대로동일한 객체의 메서드에서 속성을 참조하려면 어떻게해야합니까?
post.url = '<?php echo BASE_ADMIN . $postType ?>';
$(document).ready(function() {
$("#listing").aJqueryPlugin({
...
// Buttons and their callbacks
buttons : [
{name: 'Add', bclass: 'add', onpress : post.add},
{name: 'Edit', bclass: 'edit', onpress : post.edit},
],
...
});
라인
post.url = ....
동작합니다을 : 다른 코드에서
var post = {
url: 'http://myurl.com',
add: function() {
window.location = this.url + '/add';
},
edit: function() {
window.location = this.url + '/edit';
}
};
어딘가합니다 (이 키워드를 참고). 속성이 인 게시물이 업데이트되었습니다.
그러나, 나는이 클릭하면 또는 편집 버튼을 추가하고 내가 그 기능을 입력 this.url는 정의되지 않은이 때문에 참조 버튼 대신 포스트 개체입니다. 왜? 그러면 URL 속성을 콜백에서 참조하려면 어떻게해야합니까?
"클로저"또는 "Function.bind"를 참조하십시오. –
이것은 클로저와 관련이 없지만 실행 컨텍스트와 관련이 있습니다. – Christoph
[Javascript : Object literal reference of this '] 대신 자신의 키 함수에'Literal reference '가 중복 될 수 있습니다. http://stackoverflow.com/questions/10711064/javascript-object-literal-reference-in-own-keys-function-instead -of-this) – Bergi