Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Question

Post Timeline

added 117 characters in body
Source Link
Not A Robot
  • 2.7k
  • 2
  • 20
  • 37

How are function declarations handled?

var abc = '';
if(1 === 0){
 function a(){
 abc = 7;
 }
}else if('a' === 'a'){
 function a(){
 abc = 19;
 }
}else if('foo' === 'bar'){
 function a(){
 abc = 'foo';
 }
} 
a();
document.write(abc); //writes "foo" even though 'foo' !== 'bar'

var abc = '';
if (1 === 0) {
 function a() {
 abc = 7;
 }
} else if ('a' === 'a') {
 function a() {
 abc = 19;
 }
} else if ('foo' === 'bar') {
 function a() {
 abc = 'foo';
 }
}
a();
document.write(abc); //writes "foo" even though 'foo' !== 'bar'

This example produces different outputs in Chrome and Firefox. Chrome outputs foo while FF outputs 19.

How are function declarations handled?

var abc = '';
if(1 === 0){
 function a(){
 abc = 7;
 }
}else if('a' === 'a'){
 function a(){
 abc = 19;
 }
}else if('foo' === 'bar'){
 function a(){
 abc = 'foo';
 }
} 
a();
document.write(abc); //writes "foo" even though 'foo' !== 'bar'

This example produces different outputs in Chrome and Firefox. Chrome outputs foo while FF outputs 19.

How are function declarations handled?

var abc = '';
if (1 === 0) {
 function a() {
 abc = 7;
 }
} else if ('a' === 'a') {
 function a() {
 abc = 19;
 }
} else if ('foo' === 'bar') {
 function a() {
 abc = 'foo';
 }
}
a();
document.write(abc); //writes "foo" even though 'foo' !== 'bar'

This example produces different outputs in Chrome and Firefox. Chrome outputs foo while FF outputs 19.

deleted 555 characters in body
Source Link
HellaMad
  • 5.4k
  • 6
  • 34
  • 54

How are function declarations handled? I've heard that the parser looks for declarations before executing any other code, which seems to be the case in the following example:

var abc = '';
if(1 === 0){
 function a(){
 abc = 7;
 }
}else if('a' === 'a'){
 function a(){
 abc = 19;
 }
}else if('foo' === 'bar'){
 function a(){
 abc = 'foo';
 }
} 
a();
document.write(abc); //writes "foo" even though 'foo' !== 'bar'

EDIT
Apparently the former This example produces different outputs in Chrome and Firefox. Chrome outputs foo while FF outputs 19.


However, we can use anonymous functions to circumvent this behavior:

var abc = '';
if(1 === 0) {
 var a = function(){
 abc = 7;
 }
}else if ('a' === 'a'){
 var a = function(){
 abc = 19;
 }
}else if('foo' === 'bar'){
 var a = function(){
 abc = 'foo';
 }
}
a();
document.write(abc);​ //Writes "19", which is what you'd probably expect.

Is my thinking correct? Are there any rules to keep in mind when declaring functions?

How are function declarations handled? I've heard that the parser looks for declarations before executing any other code, which seems to be the case in the following example:

var abc = '';
if(1 === 0){
 function a(){
 abc = 7;
 }
}else if('a' === 'a'){
 function a(){
 abc = 19;
 }
}else if('foo' === 'bar'){
 function a(){
 abc = 'foo';
 }
} 
a();
document.write(abc); //writes "foo" even though 'foo' !== 'bar'

EDIT
Apparently the former example produces different outputs in Chrome and Firefox. Chrome outputs foo while FF outputs 19.


However, we can use anonymous functions to circumvent this behavior:

var abc = '';
if(1 === 0) {
 var a = function(){
 abc = 7;
 }
}else if ('a' === 'a'){
 var a = function(){
 abc = 19;
 }
}else if('foo' === 'bar'){
 var a = function(){
 abc = 'foo';
 }
}
a();
document.write(abc);​ //Writes "19", which is what you'd probably expect.

Is my thinking correct? Are there any rules to keep in mind when declaring functions?

How are function declarations handled?

var abc = '';
if(1 === 0){
 function a(){
 abc = 7;
 }
}else if('a' === 'a'){
 function a(){
 abc = 19;
 }
}else if('foo' === 'bar'){
 function a(){
 abc = 'foo';
 }
} 
a();
document.write(abc); //writes "foo" even though 'foo' !== 'bar'

This example produces different outputs in Chrome and Firefox. Chrome outputs foo while FF outputs 19.

deleted 168 characters in body
Source Link
HellaMad
  • 5.4k
  • 6
  • 34
  • 54

How are function declarations handled? I've heard that the parser looks for declarations before executing any other code, which seems to be the case in the following example:

var abc = '';
if(1 === 0){
 function a(){
 abc = 7;
 }
}else if('a' === 'a'){
 function a(){
 abc = 19;
 }
}else if('foo' === 'bar'){
 function a(){
 abc = 'foo';
 }
} 
a();
document.write(abc); //writes "foo" even though 'foo' !== 'bar'

EDIT
Apparently the former example produces different outputs in Chrome and Firefox. Chrome outputs foo while FF outputs 19.


However, we can use anonymous functions to circumvent this behavior:

var abc = '';
if(1 === 0) {
 var a = function(){
 abc = 7;
 }
}else if ('a' === 'a'){
 var a = function(){
 abc = 19;
 }
}else if('foo' === 'bar'){
 var a = function(){
 abc = 'foo';
 }
}
a();
document.write(abc);​ //Writes "19", which is what you'd probably expect.

Is my thinking correct? Are there any rules to keep in mind when declaring functions?

Also, how does passing parameters to anonymous functions in the form var a = function(){...} work? Can you simply use something like var a = function(arg){...}?

How are function declarations handled? I've heard that the parser looks for declarations before executing any other code, which seems to be the case in the following example:

var abc = '';
if(1 === 0){
 function a(){
 abc = 7;
 }
}else if('a' === 'a'){
 function a(){
 abc = 19;
 }
}else if('foo' === 'bar'){
 function a(){
 abc = 'foo';
 }
} 
a();
document.write(abc); //writes "foo" even though 'foo' !== 'bar'

EDIT
Apparently the former example produces different outputs in Chrome and Firefox. Chrome outputs foo while FF outputs 19.


However, we can use anonymous functions to circumvent this behavior:

var abc = '';
if(1 === 0) {
 var a = function(){
 abc = 7;
 }
}else if ('a' === 'a'){
 var a = function(){
 abc = 19;
 }
}else if('foo' === 'bar'){
 var a = function(){
 abc = 'foo';
 }
}
a();
document.write(abc);​ //Writes "19", which is what you'd probably expect.

Is my thinking correct? Are there any rules to keep in mind when declaring functions?

Also, how does passing parameters to anonymous functions in the form var a = function(){...} work? Can you simply use something like var a = function(arg){...}?

How are function declarations handled? I've heard that the parser looks for declarations before executing any other code, which seems to be the case in the following example:

var abc = '';
if(1 === 0){
 function a(){
 abc = 7;
 }
}else if('a' === 'a'){
 function a(){
 abc = 19;
 }
}else if('foo' === 'bar'){
 function a(){
 abc = 'foo';
 }
} 
a();
document.write(abc); //writes "foo" even though 'foo' !== 'bar'

EDIT
Apparently the former example produces different outputs in Chrome and Firefox. Chrome outputs foo while FF outputs 19.


However, we can use anonymous functions to circumvent this behavior:

var abc = '';
if(1 === 0) {
 var a = function(){
 abc = 7;
 }
}else if ('a' === 'a'){
 var a = function(){
 abc = 19;
 }
}else if('foo' === 'bar'){
 var a = function(){
 abc = 'foo';
 }
}
a();
document.write(abc);​ //Writes "19", which is what you'd probably expect.

Is my thinking correct? Are there any rules to keep in mind when declaring functions?

added 155 characters in body
Source Link
HellaMad
  • 5.4k
  • 6
  • 34
  • 54
Loading
added 1 characters in body
Source Link
HellaMad
  • 5.4k
  • 6
  • 34
  • 54
Loading
added 12 characters in body
Source Link
HellaMad
  • 5.4k
  • 6
  • 34
  • 54
Loading
Source Link
HellaMad
  • 5.4k
  • 6
  • 34
  • 54
Loading
lang-js

AltStyle によって変換されたページ (->オリジナル) /