익명 함수에 레이블을 지정하기위한 Javascript '콜론'?
이 코드는 무엇을 참조합니까?
queryString: function() {
//some code
}
WebConsole (Firefox)에서 테스트했지만 실행되지 않으므로 function queryString() {}
.
그래서 정확히 무엇입니까?
거기에 일부 코드가 누락되었지만 다음과 같은 객체 선언의 일부라고 가정합니다.
var obj = {
queryString: function() {
//some code
}
};
obj.queryString();
객체 리터럴의 속성으로 함수를 할당합니다. 다음과 같습니다.
var obj = {};
obj.queryString = function() { ... };
obj.queryString();
일반적으로 객체 리터럴 구문은 다음과 같습니다.
{ key: value, otherKey: otherValue };
따라서 이것이 콘솔에서 작동하지 않는 이유 {}
는 개체 리터럴을 나타내는 문자로 묶이지 않았기 때문 입니다. 그리고이 구문은 객체 리터럴에서만 유효합니다.
이것은 아마도 다음과 같은지도 / 객체 선언 안에있을 것입니다 :
var obj = {
queryString: function() {
alert('here');
},
eggs: function() {
alert('another function');
}
};
obj.queryString();
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label 레이블입니다.
var i, j;
loop1:
for (i = 0; i < 3; i++) { //The first for statement is labeled "loop1"
loop2:
for (j = 0; j < 3; j++) { //The second for statement is labeled "loop2"
if (i == 1 && j == 1) {
continue loop1;
}
console.log("i = " + i + ", j = " + j);
}
}
// Output is:
// "i = 0, j = 0"
// "i = 0, j = 1"
// "i = 0, j = 2"
// "i = 1, j = 0"
// "i = 2, j = 0"
// "i = 2, j = 1"
// "i = 2, j = 2"
// Notice how it skips both "i = 1, j = 1" and "i = 1, j = 2"
는 :
객체 및 그 속성을 정의 할 때 사용된다.
var obj = {
queryString: function() {
//some code
}
}
이제 obj.queryString
당신의 기능입니다.
무엇
queryString: function() {
//some code
}
의미는 queryString ()을 사용하여 참조하는 함수를 호출 할 수 있다는 것입니다. 이러한 종류의 참조는 일반적으로 자바 스크립트에서 클래스 (또는 의사 클래스; P)를 정의하려는 경우에 사용됩니다. 이 같은,
var application= { namespace: {} };
application.namespace.class_name = function(){
function constructor(){
return {
exposed_property1 : property1,
exposed_property2 : property2,
...
...
}
}
//Write property/functions that you want to expose.
// Write rest of the function that you want private as function private(){}
};
이제 코드의 다른 부분에서 class_name에 대한 객체를 만들고이를 사용하여 property1, property2 등에 액세스 할 수 있습니다.
참고 URL : https://stackoverflow.com/questions/9384865/javascript-colon-for-labeling-anonymous-functions
'program story' 카테고리의 다른 글
jquery 문서 내부 또는 외부 함수 준비 (0) | 2020.12.12 |
---|---|
파이썬의 정적 / 클래스 변수에 대한 __getattr__ (0) | 2020.12.12 |
명령 줄을 통해 GNU make 변수에 추가 (0) | 2020.12.11 |
SessionTimeout : web.xml 대 session.maxInactiveInterval () (0) | 2020.12.11 |
PHP cURL HTTP PUT (0) | 2020.12.11 |