Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.49 KB | None | 0 0
  1. var functionOne = function() {
  2. // Some code
  3. };
  4.  
  5. function functionTwo() {
  6. // Some code
  7. }
  8.  
  9. <script>
  10. // Error
  11. functionOne();
  12.  
  13. var functionOne = function() {
  14. };
  15. </script>
  16.  
  17. <script>
  18. // No error
  19. functionTwo();
  20.  
  21. function functionTwo() {
  22. }
  23. </script>
  24.  
  25. <script>
  26. "use strict";
  27. if (test) {
  28. // Error
  29. function functionThree() { doSomething(); }
  30. }
  31. </script>
  32.  
  33. function xyz(){
  34. function abc(){};
  35. // abc is defined here...
  36. }
  37. // ...but not here
  38.  
  39. var xyz = function abc(){};
  40.  
  41. var xyz = function abc(){
  42. // xyz is visible here
  43. // abc is visible here
  44. }
  45. // xyz is visible here
  46. // abc is undefined here
  47.  
  48. function abc(){};
  49. var xyz = abc;
  50.  
  51. console.log(xyz === abc); // prints "true"
  52.  
  53. function abc(){};
  54. console.log(abc.name); // prints "abc"
  55.  
  56. var abc = function(){};
  57. console.log(abc.name); // prints ""
  58.  
  59. // assume really.long.external.scoped is {}
  60. really.long.external.scoped.name = function shortcut(n){
  61. // let's call itself recursively:
  62. shortcut(n - 1);
  63. // ...
  64. // let's pass itself as a callback:
  65. someFunction(shortcut);
  66. // ...
  67. }
  68.  
  69. function abc(){}
  70.  
  71. // we can call it here
  72. abc(); // works
  73. // yet it is defined down there
  74. function abc(){}
  75. // we can call it again
  76. abc(); // works
  77.  
  78. var xyz = function(){};
  79.  
  80. // we can't call it here
  81. xyz(); // UNDEFINED!!!
  82. // now it is defined
  83. xyz = function(){}
  84. // we can call it here
  85. xyz(); // works
  86.  
  87. var xyz = function abc(){};
  88. console.log(xyz.name); // prints "abc"
  89.  
  90. var abc = function(){};
  91.  
  92. abc = function(){};
  93.  
  94. function abc(){};
  95.  
  96. alert(typeof foo); // 'function', it's already available
  97. alert(typeof bar); // 'undefined'
  98. function foo () {}
  99. var bar = function () {};
  100. alert(typeof bar); // 'function'
  101.  
  102. function test () {}
  103. test = null;
  104.  
  105. (function(){
  106. var exports = {};
  107.  
  108. function privateUtil() {
  109. ...
  110. }
  111.  
  112. exports.publicUtil = function() {
  113. ...
  114. };
  115.  
  116. return exports;
  117. })();
  118.  
  119. function x() {
  120. console.log('x');
  121. }
  122.  
  123. x(); // Works even though it's above the declaration
  124. function x() {
  125. console.log('x');
  126. }
  127.  
  128. if (someCondition) {
  129. function foo() { // <===== INVALID AND WILL FAIL ON
  130. } // MANY ENGINES
  131. }
  132.  
  133. var y = function () {
  134. console.log('y');
  135. };
  136.  
  137. var z = function w() {
  138. console.log('zw')
  139. };
  140.  
  141. var z = function w() {
  142. console.log(typeof w); // "function"
  143. };
  144. console.log(typeof w); // "undefined"
  145.  
  146. var MyNamespace = {}
  147. MyNamespace.foo= function() {
  148.  
  149. }
  150.  
  151. var MyNamespace {
  152. foo: function() {
  153. },
  154. ...
  155. }
  156.  
  157. functionTwo();
  158. function functionTwo() {
  159. }
  160.  
  161. functionOne(); --------------- var functionOne;
  162. | is actually | functionOne();
  163. var functionOne = function(){ | interpreted |-->
  164. }; | like | functionOne = function(){
  165. --------------- };
  166.  
  167. functionTwo(); --------------- function functionTwo() {
  168. | is actually | };
  169. function functionTwo() { | interpreted |-->
  170. } | like | functionTwo();
  171. ---------------
  172.  
  173. if (condition){
  174. function myfunction(){
  175. // some code
  176. }
  177. }
  178.  
  179. if (condition){
  180. var myfunction = function (){
  181. // some code
  182. }
  183. }
  184.  
  185. var foo = 1;
  186. function bar() {
  187. if (!foo) {
  188. var foo = 10 }
  189. return foo; }
  190. bar() // 10
  191.  
  192. function f() {
  193. return a;
  194. function a() {return 1};
  195. var a = 4;
  196. function a() {return 2}}
  197. f()() // 2
  198.  
  199. function f() {
  200. return a;
  201. var a = 4;
  202. function a() {return 1};
  203. function a() {return 2}}
  204. f()() // 2
  205.  
  206. function f() {
  207. var a = 4;
  208. function a() {return 1};
  209. function a() {return 2};
  210. return a; }
  211. f() // 4
  212.  
  213. var a = 1;
  214. function b() {
  215. a = 10;
  216. return;
  217. function a() {}}
  218. b();
  219. a // 1
  220.  
  221. function abc(){}
  222.  
  223. var abc = function() {};
  224.  
  225. //this will work
  226. abc(param);
  227. function abc(){}
  228.  
  229. //this would fail
  230. abc(param);
  231. var abc = function() {}
  232.  
  233. var ninja = {
  234. yell: function(n){
  235. return n > 0 ? ninja.yell(n-1) + "a" : "hiy";
  236. }
  237. };
  238. assert( ninja.yell(4) == "hiyaaaa", "A single object isn't too bad, either." );
  239.  
  240. var samurai = { yell: ninja.yell };
  241. var ninja = null;
  242.  
  243. try {
  244. samurai.yell(4);
  245. } catch(e){
  246. assert( false, "Uh, this isn't good! Where'd ninja.yell go?" );
  247. }
  248.  
  249. var ninja = {
  250. yell: function yell(n){
  251. return n > 0 ? yell(n-1) + "a" : "hiy";
  252. }
  253. };
  254. assert( ninja.yell(4) == "hiyaaaa", "Works as we would expect it to!" );
  255.  
  256. var samurai = { yell: ninja.yell };
  257. var ninja = {};
  258. assert( samurai.yell(4) == "hiyaaaa", "The method correctly calls itself." );
  259.  
  260. function foo() {
  261. return 3;
  262. }
  263.  
  264. //anonymous function expression
  265. var a = function() {
  266. return 3;
  267. }
  268.  
  269. //named function expression
  270. var a = function foo() {
  271. return 3;
  272. }
  273.  
  274. //self invoking function expression
  275. (function foo() {
  276. alert("hello!");
  277. })();
  278.  
  279. var objectOne = new functionOne();
  280. console.log(objectOne.__proto__); // prints "Object {}" because constructor is an anonymous function
  281.  
  282. var objectTwo = new functionTwo();
  283. console.log(objectTwo.__proto__); // prints "functionTwo {}" because constructor is a named function
  284.  
  285. var functionOne = function() {
  286. // Some code
  287. };
  288.  
  289. var one = new functionOne();
  290.  
  291. function functionTwo() {
  292. // Some code
  293. }
  294. two = new functionTwo();
  295.  
  296. [].forEach(function iterator() {});
  297.  
  298. 'use strict';
  299.  
  300. var a = function () {
  301. throw new Error();
  302. },
  303. b = function b() {
  304. throw new Error();
  305. },
  306. c = function d() {
  307. throw new Error();
  308. },
  309. e = {
  310. f: a,
  311. g: b,
  312. h: c,
  313. i: function () {
  314. throw new Error();
  315. },
  316. j: function j() {
  317. throw new Error();
  318. },
  319. k: function l() {
  320. throw new Error();
  321. }
  322. },
  323. m = (function () {
  324. return function () {
  325. throw new Error();
  326. };
  327. }()),
  328. n = (function () {
  329. return function n() {
  330. throw new Error();
  331. };
  332. }()),
  333. o = (function () {
  334. return function p() {
  335. throw new Error();
  336. };
  337. }());
  338.  
  339. console.log([a, b, c].concat(Object.keys(e).reduce(function (values, key) {
  340. return values.concat(e[key]);
  341. }, [])).concat([m, n, o]).reduce(function (logs, func) {
  342.  
  343. try {
  344. func();
  345. } catch (error) {
  346. return logs.concat('func.name: ' + func.name + 'n' +
  347. 'Trace:n' +
  348. error.stack);
  349. // Need to manually log the error object in Nitro.
  350. }
  351.  
  352. }, []).join('nn'));
  353.  
  354. func.name:
  355. Trace:
  356. Error
  357. at a (http://localhost:8000/test.js:4:11)
  358. at http://localhost:8000/test.js:47:9
  359. at Array.reduce (native)
  360. at http://localhost:8000/test.js:44:27
  361.  
  362. func.name: b
  363. Trace:
  364. Error
  365. at b (http://localhost:8000/test.js:7:15)
  366. at http://localhost:8000/test.js:47:9
  367. at Array.reduce (native)
  368. at http://localhost:8000/test.js:44:27
  369.  
  370. func.name: d
  371. Trace:
  372. Error
  373. at d (http://localhost:8000/test.js:10:15)
  374. at http://localhost:8000/test.js:47:9
  375. at Array.reduce (native)
  376. at http://localhost:8000/test.js:44:27
  377.  
  378. func.name:
  379. Trace:
  380. Error
  381. at a (http://localhost:8000/test.js:4:11)
  382. at http://localhost:8000/test.js:47:9
  383. at Array.reduce (native)
  384. at http://localhost:8000/test.js:44:27
  385.  
  386. func.name: b
  387. Trace:
  388. Error
  389. at b (http://localhost:8000/test.js:7:15)
  390. at http://localhost:8000/test.js:47:9
  391. at Array.reduce (native)
  392. at http://localhost:8000/test.js:44:27
  393.  
  394. func.name: d
  395. Trace:
  396. Error
  397. at d (http://localhost:8000/test.js:10:15)
  398. at http://localhost:8000/test.js:47:9
  399. at Array.reduce (native)
  400. at http://localhost:8000/test.js:44:27
  401.  
  402. func.name:
  403. Trace:
  404. Error
  405. at e.i (http://localhost:8000/test.js:17:19)
  406. at http://localhost:8000/test.js:47:9
  407. at Array.reduce (native)
  408. at http://localhost:8000/test.js:44:27
  409.  
  410. func.name: j
  411. Trace:
  412. Error
  413. at j (http://localhost:8000/test.js:20:19)
  414. at http://localhost:8000/test.js:47:9
  415. at Array.reduce (native)
  416. at http://localhost:8000/test.js:44:27
  417.  
  418. func.name: l
  419. Trace:
  420. Error
  421. at l (http://localhost:8000/test.js:23:19)
  422. at http://localhost:8000/test.js:47:9
  423. at Array.reduce (native)
  424. at http://localhost:8000/test.js:44:27
  425.  
  426. func.name:
  427. Trace:
  428. Error
  429. at http://localhost:8000/test.js:28:19
  430. at http://localhost:8000/test.js:47:9
  431. at Array.reduce (native)
  432. at http://localhost:8000/test.js:44:27
  433.  
  434. func.name: n
  435. Trace:
  436. Error
  437. at n (http://localhost:8000/test.js:33:19)
  438. at http://localhost:8000/test.js:47:9
  439. at Array.reduce (native)
  440. at http://localhost:8000/test.js:44:27
  441.  
  442. func.name: p
  443. Trace:
  444. Error
  445. at p (http://localhost:8000/test.js:38:19)
  446. at http://localhost:8000/test.js:47:9
  447. at Array.reduce (native)
  448. at http://localhost:8000/test.js:44:27 test.js:42
  449.  
  450. func.name:
  451. Trace:
  452. a@http://localhost:8000/test.js:4:5
  453. @http://localhost:8000/test.js:47:9
  454. @http://localhost:8000/test.js:54:1
  455.  
  456.  
  457. func.name: b
  458. Trace:
  459. b@http://localhost:8000/test.js:7:9
  460. @http://localhost:8000/test.js:47:9
  461. @http://localhost:8000/test.js:54:1
  462.  
  463.  
  464. func.name: d
  465. Trace:
  466. d@http://localhost:8000/test.js:10:9
  467. @http://localhost:8000/test.js:47:9
  468. @http://localhost:8000/test.js:54:1
  469.  
  470.  
  471. func.name:
  472. Trace:
  473. a@http://localhost:8000/test.js:4:5
  474. @http://localhost:8000/test.js:47:9
  475. @http://localhost:8000/test.js:54:1
  476.  
  477.  
  478. func.name: b
  479. Trace:
  480. b@http://localhost:8000/test.js:7:9
  481. @http://localhost:8000/test.js:47:9
  482. @http://localhost:8000/test.js:54:1
  483.  
  484.  
  485. func.name: d
  486. Trace:
  487. d@http://localhost:8000/test.js:10:9
  488. @http://localhost:8000/test.js:47:9
  489. @http://localhost:8000/test.js:54:1
  490.  
  491.  
  492. func.name:
  493. Trace:
  494. e.i@http://localhost:8000/test.js:17:13
  495. @http://localhost:8000/test.js:47:9
  496. @http://localhost:8000/test.js:54:1
  497.  
  498.  
  499. func.name: j
  500. Trace:
  501. j@http://localhost:8000/test.js:20:13
  502. @http://localhost:8000/test.js:47:9
  503. @http://localhost:8000/test.js:54:1
  504.  
  505.  
  506. func.name: l
  507. Trace:
  508. l@http://localhost:8000/test.js:23:13
  509. @http://localhost:8000/test.js:47:9
  510. @http://localhost:8000/test.js:54:1
  511.  
  512.  
  513. func.name:
  514. Trace:
  515. m</<@http://localhost:8000/test.js:28:13
  516. @http://localhost:8000/test.js:47:9
  517. @http://localhost:8000/test.js:54:1
  518.  
  519.  
  520. func.name: n
  521. Trace:
  522. n@http://localhost:8000/test.js:33:13
  523. @http://localhost:8000/test.js:47:9
  524. @http://localhost:8000/test.js:54:1
  525.  
  526.  
  527. func.name: p
  528. Trace:
  529. p@http://localhost:8000/test.js:38:13
  530. @http://localhost:8000/test.js:47:9
  531. @http://localhost:8000/test.js:54:1
  532.  
  533. func.name: undefined
  534. Trace:
  535. Error
  536. at a (http://localhost:8000/test.js:4:5)
  537. at Anonymous function (http://localhost:8000/test.js:47:9)
  538. at Global code (http://localhost:8000/test.js:42:1)
  539.  
  540.  
  541. func.name: undefined
  542. Trace:
  543. Error
  544. at b (http://localhost:8000/test.js:7:9)
  545. at Anonymous function (http://localhost:8000/test.js:47:9)
  546. at Global code (http://localhost:8000/test.js:42:1)
  547.  
  548.  
  549. func.name: undefined
  550. Trace:
  551. Error
  552. at d (http://localhost:8000/test.js:10:9)
  553. at Anonymous function (http://localhost:8000/test.js:47:9)
  554. at Global code (http://localhost:8000/test.js:42:1)
  555.  
  556.  
  557. func.name: undefined
  558. Trace:
  559. Error
  560. at a (http://localhost:8000/test.js:4:5)
  561. at Anonymous function (http://localhost:8000/test.js:47:9)
  562. at Global code (http://localhost:8000/test.js:42:1)
  563.  
  564.  
  565. func.name: undefined
  566. Trace:
  567. Error
  568. at b (http://localhost:8000/test.js:7:9)
  569. at Anonymous function (http://localhost:8000/test.js:47:9)
  570. at Global code (http://localhost:8000/test.js:42:1)
  571.  
  572.  
  573. func.name: undefined
  574. Trace:
  575. Error
  576. at d (http://localhost:8000/test.js:10:9)
  577. at Anonymous function (http://localhost:8000/test.js:47:9)
  578. at Global code (http://localhost:8000/test.js:42:1)
  579.  
  580.  
  581. func.name: undefined
  582. Trace:
  583. Error
  584. at e.i (http://localhost:8000/test.js:17:13)
  585. at Anonymous function (http://localhost:8000/test.js:47:9)
  586. at Global code (http://localhost:8000/test.js:42:1)
  587.  
  588.  
  589. func.name: undefined
  590. Trace:
  591. Error
  592. at j (http://localhost:8000/test.js:20:13)
  593. at Anonymous function (http://localhost:8000/test.js:47:9)
  594. at Global code (http://localhost:8000/test.js:42:1)
  595.  
  596.  
  597. func.name: undefined
  598. Trace:
  599. Error
  600. at l (http://localhost:8000/test.js:23:13)
  601. at Anonymous function (http://localhost:8000/test.js:47:9)
  602. at Global code (http://localhost:8000/test.js:42:1)
  603.  
  604.  
  605. func.name: undefined
  606. Trace:
  607. Error
  608. at Anonymous function (http://localhost:8000/test.js:28:13)
  609. at Anonymous function (http://localhost:8000/test.js:47:9)
  610. at Global code (http://localhost:8000/test.js:42:1)
  611.  
  612.  
  613. func.name: undefined
  614. Trace:
  615. Error
  616. at n (http://localhost:8000/test.js:33:13)
  617. at Anonymous function (http://localhost:8000/test.js:47:9)
  618. at Global code (http://localhost:8000/test.js:42:1)
  619.  
  620.  
  621. func.name: undefined
  622. Trace:
  623. Error
  624. at p (http://localhost:8000/test.js:38:13)
  625. at Anonymous function (http://localhost:8000/test.js:47:9)
  626. at Global code (http://localhost:8000/test.js:42:1)
  627.  
  628. func.name:
  629. Trace:
  630. a@http://localhost:8000/test.js:4:22
  631. http://localhost:8000/test.js:47:13
  632. reduce@[native code]
  633. global code@http://localhost:8000/test.js:44:33
  634.  
  635. func.name: b
  636. Trace:
  637. b@http://localhost:8000/test.js:7:26
  638. http://localhost:8000/test.js:47:13
  639. reduce@[native code]
  640. global code@http://localhost:8000/test.js:44:33
  641.  
  642. func.name: d
  643. Trace:
  644. d@http://localhost:8000/test.js:10:26
  645. http://localhost:8000/test.js:47:13
  646. reduce@[native code]
  647. global code@http://localhost:8000/test.js:44:33
  648.  
  649. func.name:
  650. Trace:
  651. a@http://localhost:8000/test.js:4:22
  652. http://localhost:8000/test.js:47:13
  653. reduce@[native code]
  654. global code@http://localhost:8000/test.js:44:33
  655.  
  656. func.name: b
  657. Trace:
  658. b@http://localhost:8000/test.js:7:26
  659. http://localhost:8000/test.js:47:13
  660. reduce@[native code]
  661. global code@http://localhost:8000/test.js:44:33
  662.  
  663. func.name: d
  664. Trace:
  665. d@http://localhost:8000/test.js:10:26
  666. http://localhost:8000/test.js:47:13
  667. reduce@[native code]
  668. global code@http://localhost:8000/test.js:44:33
  669.  
  670. func.name:
  671. Trace:
  672. i@http://localhost:8000/test.js:17:30
  673. http://localhost:8000/test.js:47:13
  674. reduce@[native code]
  675. global code@http://localhost:8000/test.js:44:33
  676.  
  677. func.name: j
  678. Trace:
  679. j@http://localhost:8000/test.js:20:30
  680. http://localhost:8000/test.js:47:13
  681. reduce@[native code]
  682. global code@http://localhost:8000/test.js:44:33
  683.  
  684. func.name: l
  685. Trace:
  686. l@http://localhost:8000/test.js:23:30
  687. http://localhost:8000/test.js:47:13
  688. reduce@[native code]
  689. global code@http://localhost:8000/test.js:44:33
  690.  
  691. func.name:
  692. Trace:
  693. http://localhost:8000/test.js:28:30
  694. http://localhost:8000/test.js:47:13
  695. reduce@[native code]
  696. global code@http://localhost:8000/test.js:44:33
  697.  
  698. func.name: n
  699. Trace:
  700. n@http://localhost:8000/test.js:33:30
  701. http://localhost:8000/test.js:47:13
  702. reduce@[native code]
  703. global code@http://localhost:8000/test.js:44:33
  704.  
  705. func.name: p
  706. Trace:
  707. p@http://localhost:8000/test.js:38:30
  708. http://localhost:8000/test.js:47:13
  709. reduce@[native code]
  710. global code@http://localhost:8000/test.js:44:33
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement