세 번째 함수는 첫 번째 함수에서 두 번째 함수를 실행하고 있습니다.javascript 다른 함수에서 함수 실행
function first() {
function third() {
}
}
function second() {
third();
}
두 번째로 세 번째로 올바르게 만들려면 어떻게해야합니까?
감사합니다.
세 번째 함수는 첫 번째 함수에서 두 번째 함수를 실행하고 있습니다.javascript 다른 함수에서 함수 실행
function first() {
function third() {
}
}
function second() {
third();
}
두 번째로 세 번째로 올바르게 만들려면 어떻게해야합니까?
감사합니다.
첫 번째 기능을 설정하는 방법과 액세스 방법에 따라 다릅니다. 기본적으로 세 번째 방법은 처음부터 사적인 방법입니다. 먼저 세 번째 전화를 걸 때 공개 방법이 필요합니다. 이 방법은 초 단위로 호출됩니다. 이 작업을 수행하는 여러 가지 방법이 있습니다
이를 염두에 관해서 하나 ...
편집 : 나는 반복해서 그 방법이 아니 "PARAM"조금 더 매개 변수를 지명했다. 당신은 세 번째 개인 것에 대해 걱정하지 않는 경우
function first()
{
// define first's private
var third = function(third_param)
{
alert(third_param);
}
// define first's public that calls the private
this.callThird = function (call_third_param)
{
third(call_third_param);
}
}
function second()
{
// get an instance of first
var temp = new first();
// call the public method on it
temp.callThird('argument');
}
, 당신은 당신이하려고하는 경우,이 또한 음부
function first()
{
// the "this", makes it public
this.third = function(third_param)
{
alert(third_param);
}
}
function second()
{
// get an instance of first
var temp = new first();
// call the public method on it
temp.third('argument');
}
를 사용하지 않는
function first() { }
// public method
first.prototype.third = function(third_param)
{
alert(third_param);
}
function second()
{
// get an instance of first
var temp = new first();
// call the public method on it
temp.third('argument');
}
또는,이 방법을 수행 할 수 있습니다 네임 스페이스와 같은 일을하기 위해서, 당신은 이것을 할 수있다. (비공개도 아니다.)
// define an object and name it first
var first =
{
// give first a public variable but make it a function with an arg
third : function(third_param)
{
alert(third_param);
}
}
function second()
{
// call the third method on the first variable
first.third('argument');
}
그러나 아마도 가장 좋은 방법은() (세 번째) 호출 단지가 첫 번째() 및 초 후 왜 함수) (첫번째 아래에 중첩되지 않음) (제 3하지 않는
var first = (function() {
// define first's private function, store it in 'third', a private variable
var third = function (third_param) {
alert(third_param);
};
// return this object to the 'first' variable
return { // define first's public that calls the private
'callThird': function (call_third_param) {
third(call_third_param);
}
};
}()); // this line runs the function, closure'ing over the privates and
// returning an object (that has access to the privates) to the 'first' variable
function second() {
// usage:
first.callThird("Hello World!");
}
이것은 굉장합니다. 고맙습니다. – Mark
자바 스크립트가 쉽게 허용하지 않습니다. 왜 그렇게해야할까요? 먼저이를 수행하기 위해 객체 또는 프로토 타입을 사용할 수 있습니다.
function first(){
third();
}
function second(){
}
function third(){
second();
}
을의 네임 통해이며, ?
function first(){
third();
}
function second(){
third();
}
function third(){
}
가장 일반적인 방법은 두 가지 기능에 공통적 인 기능을 끄집어 내고 필요한 곳에서 호출하는 것입니다.
var thirdf = function third() {
}
function first() {
thirdf();
}
function second() {
thirdf();
}
처음으로 3을 만듭니다. 귀하의 예를 보이는 경우 제안은 더 의미가보기 다음과 같습니다 : 로컬있는 모든 변수가없는 경우에 첫 번째 (내가 foo는 사용 내 예를 들어, 달리), 정의되지에는 지점이
function first(foo) { // notice that foo is local to first function third() { // do something with foo in the outer scope } return third; } function second(bar) { bar(); // call whatever gets passed in } second(first()); // third (created inside first) gets passed to second
없습니다 세 번째로 전역 네임 스페이스에서 다음과 같이하십시오.
function first() { } function second() { third(); } function third() { }
흥미로운 접근법 –
"왜 이렇게 하시겠습니까?"
first.toString()
당신의 전체 소스를 줄 것이다 : 질문, 당신은 당신이 소유하지 않는 일부 기존 코드 내부에 "개인"기능을 액세스하는 등의 합법적 인 이유가이 작업을 수행 할 필요가 가정, 여기에 해킹입니다 기능.당신이 정말로 필요하지 않은 경우 이하지 마십시오,
var prober = new Function(
first.toString().replace("{", "{ return third;") + "return first()");
var third = prober();
third(); // yay!
을하지만 : 당신은 세 번째 기능에 당신에게 참조를 반환하는 함수에 코드의 비트를 삽입 할 수 있습니다. 교육적인 목적으로 언급하고 싶었던 큰 해킹입니다.
나는 이것을 투표하지 않을 것이다. 그러나 이것은 매우 나쁜 생각이다 :) –
무엇이 잘못 되었습니까? –
@ 제임스, 제 3 개인 메서드는 첫 번째와 두 번째는 그것을 호출 할 수 없습니다 –
난 그냥 무슨 일이 일어나고 있는지 알고 싶습니다. :) 그들은 그것을 실행하고 오류가 발생 했나요? –