Advertisement
Guest User

Untitled

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