Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var functionOne = function() {
- // Some code
- };
- function functionTwo() {
- // Some code
- }
- if (test) {
- // Error or misbehavior
- function functionThree() { doSomething(); }
- }
- function xyz(){
- function abc(){};
- // abc is defined here...
- }
- // ...but not here
- var xyz = function abc(){};
- var xyz = function abc(){
- // xyz is visible here
- // abc is visible here
- }
- // xyz is visible here
- // abc is undefined here
- function abc(){};
- var xyz = abc;
- console.log(xyz === abc); // prints "true"
- function abc(){};
- console.log(abc.name); // prints "abc"
- var abc = function(){};
- console.log(abc.name); // prints ""
- // Assume really.long.external.scoped is {}
- really.long.external.scoped.name = function shortcut(n){
- // Let it call itself recursively:
- shortcut(n - 1);
- // ...
- // Let it pass itself as a callback:
- someFunction(shortcut);
- // ...
- }
- function abc(){}
- // We can call it here
- abc(); // Works
- // Yet, it is defined down there.
- function abc(){}
- // We can call it again
- abc(); // Works
- // We can call it here
- abc(); // Works
- return;
- function abc(){}
- var xyz = function(){};
- // We can't call it here
- xyz(); // UNDEFINED!!!
- // Now it is defined
- xyz = function(){}
- // We can call it here
- xyz(); // works
- var xyz = function abc(){};
- console.log(xyz.name); // Prints "abc"
- var abc = function(){};
- abc = function(){};
- function abc(){};
- function x() {
- console.log('x');
- }
- x(); // Works even though it's above the declaration
- function x() {
- console.log('x');
- }
- if (someCondition) {
- function foo() { // <===== HERE THERE
- } // <===== BE DRAGONS
- }
- "use strict";
- if (someCondition) {
- foo(); // Works just fine
- function foo() {
- }
- }
- console.log(typeof foo); // "undefined" (`foo` is not in scope here
- // because it's not in the same block)
- var y = function () {
- console.log('y');
- };
- var z = function w() {
- console.log('zw')
- };
- var z = function w() {
- console.log(typeof w); // "function"
- };
- console.log(typeof w); // "undefined"
- var obj = {
- value: 0,
- get f() {
- return this.value;
- },
- set f(v) {
- this.value = v;
- }
- };
- console.log(obj.f); // 0
- console.log(typeof obj.f); // "number"
- var a = [1, 2, 3];
- var b = a.map(n => n * 2);
- console.log(b.join(", ")); // 2, 4, 6
- var a = [1, 2, 3];
- var b = a.map((n, i) => n * i);
- console.log(b.join(", ")); // 0, 2, 6
- var a = [
- {first: "Joe", last: "Bloggs"},
- {first: "Albert", last: "Bloggs"},
- {first: "Mary", last: "Albright"}
- ];
- a = a.sort((a, b) => {
- var rv = a.last.localeCompare(b.last);
- if (rv === 0) {
- rv = a.first.localeCompare(b.first);
- }
- return rv;
- });
- console.log(JSON.stringify(a));
- var o = {
- foo() {
- }
- };
- var o = {
- foo: function foo() {
- }
- };
- class Person {
- constructor(firstName, lastName) {
- this.firstName = firstName;
- this.lastName = lastName;
- }
- getFullName() {
- return this.firstName + " " + this.lastName;
- }
- }
- alert(typeof foo); // 'function', it's already available
- alert(typeof bar); // 'undefined'
- function foo () {}
- var bar = function () {};
- alert(typeof bar); // 'function'
- function test () {}
- test = null;
- (function(){
- var exports = {};
- function privateUtil() {
- ...
- }
- exports.publicUtil = function() {
- ...
- };
- return exports;
- })();
- functionTwo();
- function functionTwo() {
- }
- functionOne(); --------------- var functionOne;
- | is actually | functionOne();
- var functionOne = function(){ | interpreted |-->
- }; | like | functionOne = function(){
- --------------- };
- functionTwo(); --------------- function functionTwo() {
- | is actually | };
- function functionTwo() { | interpreted |-->
- } | like | functionTwo();
- ---------------
- if (condition){
- function myfunction(){
- // some code
- }
- }
- if (condition){
- var myfunction = function (){
- // some code
- }
- }
- var MyNamespace = {}
- MyNamespace.foo= function() {
- }
- var MyNamespace = {
- foo: function() {
- },
- ...
- }
- var foo = 1;
- function bar() {
- if (!foo) {
- var foo = 10 }
- return foo; }
- bar() // 10
- function f() {
- return a;
- function a() {return 1};
- var a = 4;
- function a() {return 2}}
- f()() // 2
- function f() {
- return a;
- var a = 4;
- function a() {return 1};
- function a() {return 2}}
- f()() // 2
- function f() {
- var a = 4;
- function a() {return 1};
- function a() {return 2};
- return a; }
- f() // 4
- var a = 1;
- function b() {
- a = 10;
- return;
- function a() {}}
- b();
- a // 1
- global_Page = 10; var global_Page; « undefined
- « Integer literal, Number Type. ------------------- global_Page = 10; « Number
- global_Page = 'Yash'; | Interpreted | global_Page = 'Yash'; « String
- « String literal, String Type. « AS « global_Page = true; « Boolean
- var global_Page = true; | | global_Page = function (){ « function
- « Boolean Type ------------------- var local_functionblock; « undefined
- global_Page = function (){ local_functionblock = 777;« Number
- var local_functionblock = 777; };
- // Assigning function as a data.
- };
- function Identifier_opt ( FormalParameterList_opt ) {
- FunctionBody | sequence of statements
- « return; Default undefined
- « return 'some data';
- }
- Scope with respect to function-block global.
- Scope with respect to page undefined | not available.
- function globalAccess() { function globalAccess() {
- } ------------------- }
- globalAccess(); | | function globalAccess() { « Re-Defined / overridden.
- localAccess(); « Hoisted As « function localAccess() {
- function globalAccess() { | | }
- localAccess(); ------------------- localAccess(); « function accessed with in globalAccess() only.
- function localAccess() { }
- } globalAccess();
- } localAccess(); « ReferenceError as the function is not defined
- 10; « literal
- (10); « Expression (10).toString() -> '10'
- var a;
- a = 10; « Expression var a.toString() -> '10'
- (function invoke() { « Expression Function
- console.log('Self Invoking'); (function () {
- }); }) () -> 'Self Invoking'
- var f;
- f = function (){ « Expression var Function
- console.log('var Function'); f () -> 'var Function'
- };
- (function selfExecuting(){
- console.log('IIFE - Immediately-Invoked Function Expression');
- }());
- var anonymous = function (){
- console.log('anonymous function Expression');
- };
- var namedExpression = function for_InternalUSE(fact){
- if(fact === 1){
- return 1;
- }
- var localExpression = function(){
- console.log('Local to the parent Function Scope');
- };
- globalExpression = function(){
- console.log('creates a new global variable, then assigned this function.');
- };
- //return; //undefined.
- return fact * for_InternalUSE( fact - 1);
- };
- namedExpression();
- globalExpression();
- var anonymous;
- var namedExpression;
- var globalExpression;
- anonymous = function (){
- console.log('anonymous function Expression');
- };
- namedExpression = function for_InternalUSE(fact){
- var localExpression;
- if(fact === 1){
- return 1;
- }
- localExpression = function(){
- console.log('Local to the parent Function Scope');
- };
- globalExpression = function(){
- console.log('creates a new global variable, then assigned this function.');
- };
- return fact * for_InternalUSE( fact - 1); // DEFAULT UNDEFINED.
- };
- namedExpression(10);
- globalExpression();
- function abc(){}
- var abc = function() {};
- //this will work
- abc(param);
- function abc(){}
- //this would fail
- abc(param);
- var abc = function() {}
- {
- member:function() { /* How do I make "this.member" a named function? */
- }
- }
- var foo = function foo() {};
- function foo() {};
- function foo() {};
- var foo = function foo() {};
- var foo = undefined;
- foo = function foo() {};
- var ninja = {
- yell: function(n){
- return n > 0 ? ninja.yell(n-1) + "a" : "hiy";
- }
- };
- assert( ninja.yell(4) == "hiyaaaa", "A single object isn't too bad, either." );
- var samurai = { yell: ninja.yell };
- var ninja = null;
- try {
- samurai.yell(4);
- } catch(e){
- assert( false, "Uh, this isn't good! Where'd ninja.yell go?" );
- }
- var ninja = {
- yell: function yell(n){
- return n > 0 ? yell(n-1) + "a" : "hiy";
- }
- };
- assert( ninja.yell(4) == "hiyaaaa", "Works as we would expect it to!" );
- var samurai = { yell: ninja.yell };
- var ninja = {};
- assert( samurai.yell(4) == "hiyaaaa", "The method correctly calls itself." );
- var functionOne = function() {
- // Some code
- };
- var one = new functionOne();
- function functionTwo() {
- // Some code
- }
- two = new functionTwo();
- var objectOne = new functionOne();
- console.log(objectOne.__proto__); // prints "Object {}" because constructor is an anonymous function
- var objectTwo = new functionTwo();
- console.log(objectTwo.__proto__); // prints "functionTwo {}" because constructor is a named function
- function outerFunction() {
- function foo() {
- return 1;
- }
- return foo();
- function foo() {
- return 2;
- }
- }
- alert(outerFunction()); // Displays 2
- function foo() { // The first function declaration is moved to top
- return 1;
- }
- function foo() { // The second function declaration is moved to top
- return 2;
- }
- function outerFunction() {
- return foo();
- }
- alert(outerFunction()); //So executing from top to bottom,
- //the last foo() returns 2 which gets displayed
- function outerFunction() {
- var foo = function() {
- return 1;
- }
- return foo();
- var foo = function() {
- return 2;
- }
- }
- alert(outerFunction()); // Displays 1
- function outerFunction() {
- var foo = undefined;
- var foo = undefined;
- foo = function() {
- return 1;
- };
- return foo ();
- foo = function() { // This function expression is not reachable
- return 2;
- };
- }
- alert(outerFunction()); // Displays 1
- if (test) {
- function x() { doSomething(); }
- }
- var today = function today() {return new Date()}
- function foo() {
- return 3;
- }
- // Anonymous function expression
- var a = function() {
- return 3;
- }
- // Named function expression
- var a = function foo() {
- return 3;
- }
- // Self-invoking function expression
- (function foo() {
- alert("hello!");
- })();
- [].forEach(function iterator() {});
- 'use strict';
- var a = function () {
- throw new Error();
- },
- b = function b() {
- throw new Error();
- },
- c = function d() {
- throw new Error();
- },
- e = {
- f: a,
- g: b,
- h: c,
- i: function () {
- throw new Error();
- },
- j: function j() {
- throw new Error();
- },
- k: function l() {
- throw new Error();
- }
- },
- m = (function () {
- return function () {
- throw new Error();
- };
- }()),
- n = (function () {
- return function n() {
- throw new Error();
- };
- }()),
- o = (function () {
- return function p() {
- throw new Error();
- };
- }());
- console.log([a, b, c].concat(Object.keys(e).reduce(function (values, key) {
- return values.concat(e[key]);
- }, [])).concat([m, n, o]).reduce(function (logs, func) {
- try {
- func();
- } catch (error) {
- return logs.concat('func.name: ' + func.name + 'n' +
- 'Trace:n' +
- error.stack);
- // Need to manually log the error object in Nitro.
- }
- }, []).join('nn'));
- func.name:
- Trace:
- Error
- at a (http://localhost:8000/test.js:4:11)
- at http://localhost:8000/test.js:47:9
- at Array.reduce (native)
- at http://localhost:8000/test.js:44:27
- func.name: b
- Trace:
- Error
- at b (http://localhost:8000/test.js:7:15)
- at http://localhost:8000/test.js:47:9
- at Array.reduce (native)
- at http://localhost:8000/test.js:44:27
- func.name: d
- Trace:
- Error
- at d (http://localhost:8000/test.js:10:15)
- at http://localhost:8000/test.js:47:9
- at Array.reduce (native)
- at http://localhost:8000/test.js:44:27
- func.name:
- Trace:
- Error
- at a (http://localhost:8000/test.js:4:11)
- at http://localhost:8000/test.js:47:9
- at Array.reduce (native)
- at http://localhost:8000/test.js:44:27
- func.name: b
- Trace:
- Error
- at b (http://localhost:8000/test.js:7:15)
- at http://localhost:8000/test.js:47:9
- at Array.reduce (native)
- at http://localhost:8000/test.js:44:27
- func.name: d
- Trace:
- Error
- at d (http://localhost:8000/test.js:10:15)
- at http://localhost:8000/test.js:47:9
- at Array.reduce (native)
- at http://localhost:8000/test.js:44:27
- func.name:
- Trace:
- Error
- at e.i (http://localhost:8000/test.js:17:19)
- at http://localhost:8000/test.js:47:9
- at Array.reduce (native)
- at http://localhost:8000/test.js:44:27
- func.name: j
- Trace:
- Error
- at j (http://localhost:8000/test.js:20:19)
- at http://localhost:8000/test.js:47:9
- at Array.reduce (native)
- at http://localhost:8000/test.js:44:27
- func.name: l
- Trace:
- Error
- at l (http://localhost:8000/test.js:23:19)
- at http://localhost:8000/test.js:47:9
- at Array.reduce (native)
- at http://localhost:8000/test.js:44:27
- func.name:
- Trace:
- Error
- at http://localhost:8000/test.js:28:19
- at http://localhost:8000/test.js:47:9
- at Array.reduce (native)
- at http://localhost:8000/test.js:44:27
- func.name: n
- Trace:
- Error
- at n (http://localhost:8000/test.js:33:19)
- at http://localhost:8000/test.js:47:9
- at Array.reduce (native)
- at http://localhost:8000/test.js:44:27
- func.name: p
- Trace:
- Error
- at p (http://localhost:8000/test.js:38:19)
- at http://localhost:8000/test.js:47:9
- at Array.reduce (native)
- at http://localhost:8000/test.js:44:27 test.js:42
- func.name:
- Trace:
- a@http://localhost:8000/test.js:4:5
- @http://localhost:8000/test.js:47:9
- @http://localhost:8000/test.js:54:1
- func.name: b
- Trace:
- b@http://localhost:8000/test.js:7:9
- @http://localhost:8000/test.js:47:9
- @http://localhost:8000/test.js:54:1
- func.name: d
- Trace:
- d@http://localhost:8000/test.js:10:9
- @http://localhost:8000/test.js:47:9
- @http://localhost:8000/test.js:54:1
- func.name:
- Trace:
- a@http://localhost:8000/test.js:4:5
- @http://localhost:8000/test.js:47:9
- @http://localhost:8000/test.js:54:1
- func.name: b
- Trace:
- b@http://localhost:8000/test.js:7:9
- @http://localhost:8000/test.js:47:9
- @http://localhost:8000/test.js:54:1
- func.name: d
- Trace:
- d@http://localhost:8000/test.js:10:9
- @http://localhost:8000/test.js:47:9
- @http://localhost:8000/test.js:54:1
- func.name:
- Trace:
- e.i@http://localhost:8000/test.js:17:13
- @http://localhost:8000/test.js:47:9
- @http://localhost:8000/test.js:54:1
- func.name: j
- Trace:
- j@http://localhost:8000/test.js:20:13
- @http://localhost:8000/test.js:47:9
- @http://localhost:8000/test.js:54:1
- func.name: l
- Trace:
- l@http://localhost:8000/test.js:23:13
- @http://localhost:8000/test.js:47:9
- @http://localhost:8000/test.js:54:1
- func.name:
- Trace:
- m</<@http://localhost:8000/test.js:28:13
- @http://localhost:8000/test.js:47:9
- @http://localhost:8000/test.js:54:1
- func.name: n
- Trace:
- n@http://localhost:8000/test.js:33:13
- @http://localhost:8000/test.js:47:9
- @http://localhost:8000/test.js:54:1
- func.name: p
- Trace:
- p@http://localhost:8000/test.js:38:13
- @http://localhost:8000/test.js:47:9
- @http://localhost:8000/test.js:54:1
- func.name: undefined
- Trace:
- Error
- at a (http://localhost:8000/test.js:4:5)
- at Anonymous function (http://localhost:8000/test.js:47:9)
- at Global code (http://localhost:8000/test.js:42:1)
- func.name: undefined
- Trace:
- Error
- at b (http://localhost:8000/test.js:7:9)
- at Anonymous function (http://localhost:8000/test.js:47:9)
- at Global code (http://localhost:8000/test.js:42:1)
- func.name: undefined
- Trace:
- Error
- at d (http://localhost:8000/test.js:10:9)
- at Anonymous function (http://localhost:8000/test.js:47:9)
- at Global code (http://localhost:8000/test.js:42:1)
- func.name: undefined
- Trace:
- Error
- at a (http://localhost:8000/test.js:4:5)
- at Anonymous function (http://localhost:8000/test.js:47:9)
- at Global code (http://localhost:8000/test.js:42:1)
- func.name: undefined
- Trace:
- Error
- at b (http://localhost:8000/test.js:7:9)
- at Anonymous function (http://localhost:8000/test.js:47:9)
- at Global code (http://localhost:8000/test.js:42:1)
- func.name: undefined
- Trace:
- Error
- at d (http://localhost:8000/test.js:10:9)
- at Anonymous function (http://localhost:8000/test.js:47:9)
- at Global code (http://localhost:8000/test.js:42:1)
- func.name: undefined
- Trace:
- Error
- at e.i (http://localhost:8000/test.js:17:13)
- at Anonymous function (http://localhost:8000/test.js:47:9)
- at Global code (http://localhost:8000/test.js:42:1)
- func.name: undefined
- Trace:
- Error
- at j (http://localhost:8000/test.js:20:13)
- at Anonymous function (http://localhost:8000/test.js:47:9)
- at Global code (http://localhost:8000/test.js:42:1)
- func.name: undefined
- Trace:
- Error
- at l (http://localhost:8000/test.js:23:13)
- at Anonymous function (http://localhost:8000/test.js:47:9)
- at Global code (http://localhost:8000/test.js:42:1)
- func.name: undefined
- Trace:
- Error
- at Anonymous function (http://localhost:8000/test.js:28:13)
- at Anonymous function (http://localhost:8000/test.js:47:9)
- at Global code (http://localhost:8000/test.js:42:1)
- func.name: undefined
- Trace:
- Error
- at n (http://localhost:8000/test.js:33:13)
- at Anonymous function (http://localhost:8000/test.js:47:9)
- at Global code (http://localhost:8000/test.js:42:1)
- func.name: undefined
- Trace:
- Error
- at p (http://localhost:8000/test.js:38:13)
- at Anonymous function (http://localhost:8000/test.js:47:9)
- at Global code (http://localhost:8000/test.js:42:1)
- func.name:
- Trace:
- a@http://localhost:8000/test.js:4:22
- http://localhost:8000/test.js:47:13
- reduce@[native code]
- global code@http://localhost:8000/test.js:44:33
- func.name: b
- Trace:
- b@http://localhost:8000/test.js:7:26
- http://localhost:8000/test.js:47:13
- reduce@[native code]
- global code@http://localhost:8000/test.js:44:33
- func.name: d
- Trace:
- d@http://localhost:8000/test.js:10:26
- http://localhost:8000/test.js:47:13
- reduce@[native code]
- global code@http://localhost:8000/test.js:44:33
- func.name:
- Trace:
- a@http://localhost:8000/test.js:4:22
- http://localhost:8000/test.js:47:13
- reduce@[native code]
- global code@http://localhost:8000/test.js:44:33
- func.name: b
- Trace:
- b@http://localhost:8000/test.js:7:26
- http://localhost:8000/test.js:47:13
- reduce@[native code]
- global code@http://localhost:8000/test.js:44:33
- func.name: d
- Trace:
- d@http://localhost:8000/test.js:10:26
- http://localhost:8000/test.js:47:13
- reduce@[native code]
- global code@http://localhost:8000/test.js:44:33
- func.name:
- Trace:
- i@http://localhost:8000/test.js:17:30
- http://localhost:8000/test.js:47:13
- reduce@[native code]
- global code@http://localhost:8000/test.js:44:33
- func.name: j
- Trace:
- j@http://localhost:8000/test.js:20:30
- http://localhost:8000/test.js:47:13
- reduce@[native code]
- global code@http://localhost:8000/test.js:44:33
- func.name: l
- Trace:
- l@http://localhost:8000/test.js:23:30
- http://localhost:8000/test.js:47:13
- reduce@[native code]
- global code@http://localhost:8000/test.js:44:33
- func.name:
- Trace:
- http://localhost:8000/test.js:28:30
- http://localhost:8000/test.js:47:13
- reduce@[native code]
- global code@http://localhost:8000/test.js:44:33
- func.name: n
- Trace:
- n@http://localhost:8000/test.js:33:30
- http://localhost:8000/test.js:47:13
- reduce@[native code]
- global code@http://localhost:8000/test.js:44:33
- func.name: p
- Trace:
- p@http://localhost:8000/test.js:38:30
- http://localhost:8000/test.js:47:13
- reduce@[native code]
- global code@http://localhost:8000/test.js:44:33
- functionOne();
- var functionOne = function() {
- // Some code
- };
- functionOne();
- function functionOne() {
- // Some code
- }
- function fn(){
- console.log("Hello");
- }
- fn();
- var fn=function(){
- console.log("Hello");
- }
- fn();
- var func = new Function("x", "y", "return x*y;");
- function secondFunction(){
- var result;
- result = func(10,20);
- console.log ( result );
- }
- secondFunction()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement