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.
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.
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?