Guest User

Untitled

a guest
Aug 29th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.19 KB | None | 0 0
  1. <script type="text/javascript">//<![CDATA[
  2. alert("I'm evaluated in the initial global execution context!");
  3.  
  4. setTimeout(function () {
  5. alert("I'm NOT evaluated in the initial global execution context.");
  6. }, 1);
  7. //]]></script>
  8.  
  9. function doWork(callbackfn, thisArg) {
  10. //...
  11. if (callbackfn != null) callbackfn.call(thisArg);
  12. }
  13.  
  14. function MyType() {
  15. this.someData = "a string";
  16. }
  17.  
  18. var instance = new MyType();
  19. // Kind of like the following, but there are more steps involved:
  20. // var instance = {};
  21. // MyType.call(instance);
  22.  
  23. <script type="text/javascript">
  24. if (true) {
  25. // Line A
  26. }
  27. </script>
  28.  
  29. <script type="text/javascript">
  30. var obj = {
  31. someData: "a string"
  32. };
  33.  
  34. function myFun() {
  35. // Line B
  36. }
  37.  
  38. obj.staticFunction = myFun;
  39.  
  40. obj.staticFunction();
  41. </script>
  42.  
  43. <script type="text/javascript">
  44. var obj = {
  45. myMethod : function () {
  46. // Line C
  47. }
  48. };
  49. var myFun = obj.myMethod;
  50. myFun();
  51. </script>
  52.  
  53. <script type="text/javascript">
  54. function myFun() {
  55. // Line D
  56. }
  57. var obj = {
  58. myMethod : function () {
  59. eval("myFun()");
  60. }
  61. };
  62. obj.myMethod();
  63. </script>
  64.  
  65. <script type="text/javascript">
  66. function myFun() {
  67. // Line E
  68. }
  69. var obj = {
  70. someData: "a string"
  71. };
  72. myFun.call(obj);
  73. </script>
  74.  
  75. document.write(this); //[object Window]
  76.  
  77. function f1()
  78. {
  79. return this;
  80. }
  81. document.write(f1()); //[object Window]
  82.  
  83. function f()
  84. {
  85. return this;
  86. }
  87.  
  88. document.write(window.f()); //[object Window]
  89.  
  90. var obj = {
  91. name: "obj",
  92. f: function () {
  93. return this + ":" + this.name;
  94. }
  95. };
  96. document.write(obj.f()); //[object Object]:obj
  97.  
  98. var obj = {
  99. name: "obj1",
  100. nestedobj: {
  101. name:"nestedobj",
  102. f: function () {
  103. return this + ":" + this.name;
  104. }
  105. }
  106. }
  107.  
  108. document.write(obj.nestedobj.f()); //[object Object]:nestedobj
  109.  
  110. var obj1 = {
  111. name: "obj1",
  112. }
  113.  
  114. function returnName() {
  115. return this + ":" + this.name;
  116. }
  117.  
  118. obj1.f = returnName; //add method to object
  119. document.write(obj1.f()); //[object Object]:obj1
  120.  
  121. var context = "global";
  122.  
  123. var obj = {
  124. context: "object",
  125. method: function () {
  126. function f() {
  127. var context = "function";
  128. return this + ":" +this.context;
  129. };
  130. return f(); //invoked without context
  131. }
  132. };
  133.  
  134. document.write(obj.method()); //[object Window]:global
  135.  
  136. /*********************************************************************
  137. 1. When you add variable to the function using this keyword, it
  138. gets added to the function prototype, thus allowing all function
  139. instances to have their own copy of the variables added.
  140. *********************************************************************/
  141. function functionDef()
  142. {
  143. this.name = "ObjDefinition";
  144. this.getName = function(){
  145. return this+":"+this.name;
  146. }
  147. }
  148.  
  149. obj1 = new functionDef();
  150. document.write(obj1.getName() + "<br />"); //[object Object]:ObjDefinition
  151.  
  152. /*********************************************************************
  153. 2. Members explicitly added to the function protorype also behave
  154. as above: all function instances have their own copy of the
  155. variable added.
  156. *********************************************************************/
  157. functionDef.prototype.version = 1;
  158. functionDef.prototype.getVersion = function(){
  159. return "v"+this.version; //see how this.version refers to the
  160. //version variable added through
  161. //prototype
  162. }
  163. document.write(obj1.getVersion() + "<br />"); //v1
  164.  
  165. /*********************************************************************
  166. 3. Illustrating that the function variables added by both above
  167. ways have their own copies across function instances
  168. *********************************************************************/
  169. functionDef.prototype.incrementVersion = function(){
  170. this.version = this.version + 1;
  171. }
  172. var obj2 = new functionDef();
  173. document.write(obj2.getVersion() + "<br />"); //v1
  174.  
  175. obj2.incrementVersion(); //incrementing version in obj2
  176. //does not affect obj1 version
  177.  
  178. document.write(obj2.getVersion() + "<br />"); //v2
  179. document.write(obj1.getVersion() + "<br />"); //v1
  180.  
  181. /*********************************************************************
  182. 4. `this` keyword refers to the immediate parent object. If you
  183. nest the object through function prototype, then `this` inside
  184. object refers to the nested object not the function instance
  185. *********************************************************************/
  186. functionDef.prototype.nestedObj = { name: 'nestedObj',
  187. getName1 : function(){
  188. return this+":"+this.name;
  189. }
  190. };
  191.  
  192. document.write(obj2.nestedObj.getName1() + "<br />"); //[object Object]:nestedObj
  193.  
  194. /*********************************************************************
  195. 5. If the method is on an object's prototype chain, `this` refers
  196. to the object the method was called on, as if the method was on
  197. the object.
  198. *********************************************************************/
  199. var ProtoObj = { fun: function () { return this.a } };
  200. var obj3 = Object.create(ProtoObj); //creating an object setting ProtoObj
  201. //as its prototype
  202. obj3.a = 999; //adding instance member to obj3
  203. document.write(obj3.fun()+"<br />");//999
  204. //calling obj3.fun() makes
  205. //ProtoObj.fun() to access obj3.a as
  206. //if fun() is defined on obj3
  207.  
  208. var myname = "global context";
  209. function SimpleFun()
  210. {
  211. this.myname = "simple function";
  212. }
  213.  
  214. var obj1 = new SimpleFun(); //adds myname to obj1
  215. //1. `new` causes `this` inside the SimpleFun() to point to the
  216. // object being constructed thus adding any member
  217. // created inside SimipleFun() using this.membername to the
  218. // object being constructed
  219. //2. And by default `new` makes function to return newly
  220. // constructed object if no explicit return value is specified
  221.  
  222. document.write(obj1.myname); //simple function
  223.  
  224. var ProtoObj = {
  225. fun: function () {
  226. return this.a;
  227. }
  228. };
  229. //Object.create() creates object with ProtoObj as its
  230. //prototype and assigns it to obj3, thus making fun()
  231. //to be the method on its prototype chain
  232.  
  233. var obj3 = Object.create(ProtoObj);
  234. obj3.a = 999;
  235. document.write(obj3.fun()); //999
  236.  
  237. //Notice that fun() is defined on obj3's prototype but
  238. //`this.a` inside fun() retrieves obj3.a
  239.  
  240. function add(inc1, inc2)
  241. {
  242. return this.a + inc1 + inc2;
  243. }
  244.  
  245. var o = { a : 4 };
  246. document.write(add.call(o, 5, 6)+"<br />"); //15
  247. //above add.call(o,5,6) sets `this` inside
  248. //add() to `o` and calls add() resulting:
  249. // this.a + inc1 + inc2 =
  250. // `o.a` i.e. 4 + 5 + 6 = 15
  251. document.write(add.apply(o, [5, 6]) + "<br />"); //15
  252. // `o.a` i.e. 4 + 5 + 6 = 15
  253.  
  254. var g = add.bind(o, 5, 6); //g: `o.a` i.e. 4 + 5 + 6
  255. document.write(g()+"<br />"); //15
  256.  
  257. var h = add.bind(o, 5); //h: `o.a` i.e. 4 + 5 + ?
  258. document.write(h(6) + "<br />"); //15
  259. // 4 + 5 + 6 = 15
  260. document.write(h() + "<br />"); //NaN
  261. //no parameter is passed to h()
  262. //thus inc2 inside add() is `undefined`
  263. //4 + 5 + undefined = NaN</code>
  264.  
  265. <script>
  266. function clickedMe() {
  267. alert(this + " : " + this.tagName + " : " + this.id);
  268. }
  269. document.getElementById("button1").addEventListener("click", clickedMe, false);
  270. document.getElementById("button2").onclick = clickedMe;
  271. document.getElementById("button5").attachEvent('onclick', clickedMe);
  272. </script>
  273.  
  274. <h3>Using `this` "directly" inside event handler or event property</h3>
  275. <button id="button1">click() "assigned" using addEventListner() </button><br />
  276. <button id="button2">click() "assigned" using click() </button><br />
  277. <button id="button3" onclick="alert(this+ ' : ' + this.tagName + ' : ' + this.id);">used `this` directly in click event property</button>
  278.  
  279. <h3>Using `this` "indirectly" inside event handler or event property</h3>
  280. <button onclick="alert((function(){return this + ' : ' + this.tagName + ' : ' + this.id;})());">`this` used indirectly, inside function <br /> defined & called inside event property</button><br />
  281.  
  282. <button id="button4" onclick="clickedMe()">`this` used indirectly, inside function <br /> called inside event property</button> <br />
  283.  
  284. IE only: <button id="button5">click() "attached" using attachEvent() </button>
  285.  
  286. this
  287.  
  288. this
  289.  
  290. this
  291.  
  292. function foo() {
  293. console.log("bar");
  294. console.log(this);
  295. }
  296. foo(); // calling the function
  297.  
  298. var myObj = {key: "Obj"};
  299. myObj.logThis = function () {
  300. // I am a method
  301. console.log(this);
  302. }
  303. myObj.logThis(); // myObj is logged
  304.  
  305. // continuing with the previous code snippet
  306.  
  307. var myVar = myObj.thisMethod;
  308. myVar();
  309. // logs either of window/global/undefined based on mode of operation
  310.  
  311. var el = document.getElementById('idOfEl');
  312. el.addEventListener('click', function() { console.log(this) });
  313. // the function called by addEventListener contains this as the reference to the element
  314. // so clicking on our element would log that element itself
  315.  
  316. function Person (name) {
  317. this.name = name;
  318. this.sayHello = function () {
  319. console.log ("Hello", this);
  320. }
  321. }
  322.  
  323. var awal = new Person("Awal");
  324. awal.sayHello();
  325. // In `awal.sayHello`, `this` contains the reference to the variable `awal`
  326.  
  327. function foo () { console.log (this, arguments); }
  328. var thisArg = {myObj: "is cool"};
  329. foo.call(thisArg, 1, 2, 3);
  330.  
  331. foo.apply(thisArg, [1,2,3])
  332.  
  333. function foo (a, b) {
  334. console.log (this, arguments);
  335. }
  336. var thisArg = {myObj: "even more cool now"};
  337. var bound = foo.bind(thisArg, 1, 2);
  338. console.log (typeof bound); // logs `function`
  339. console.log (bound);
  340. /* logs `function () { native code }` */
  341.  
  342. bound(); // calling the function returned by `.bind`
  343. // logs `{myObj: "even more cool now"}, [1, 2]`
  344.  
  345. var myObj = {
  346. hello: function () {
  347. return "world"
  348. },
  349. myMethod: function () {
  350. // copy this, variable names are case-sensitive
  351. var that = this;
  352. // callbacks ftw o/
  353. foo.bar("args", function () {
  354. // I want to call `hello` here
  355. this.hello(); // error
  356. // but `this` references to `foo` damn!
  357. // oh wait we have a backup o/
  358. that.hello(); // "world"
  359. });
  360. }
  361. };
  362.  
  363. var person = {
  364. firstName: "Penelope",
  365. lastName: "Barrymore",
  366. fullName: function () {
  367.  
  368. // We use "this" just as in the sentence above:
  369. console.log(this.firstName + " " + this.lastName);
  370.  
  371. // We could have also written:
  372. console.log(person.firstName + " " + person.lastName);
  373. }
  374. }
  375.  
  376. function foo() {
  377. // return an arrow function
  378. return (a) => {
  379. // `this` here is lexically inherited from `foo()`
  380. console.log(this.a);
  381. };
  382. }
  383. var obj1 = { a: 2 };
  384. var obj2 = { a: 3 };
  385.  
  386. var bar = foo.call(obj1);
  387. bar.call( obj2 ); // 2, not 3!
  388.  
  389. function someKindOfFunction() {
  390. this.style = 'foo';
  391. }
  392.  
  393. <script type="text/javascript">
  394. console.log(this === window); // true
  395. var foo = "bar";
  396. console.log(this.foo); // "bar"
  397. console.log(window.foo); // "bar"
  398.  
  399. >this
  400. { ArrayBuffer: [Function: ArrayBuffer],
  401. Int8Array: { [Function: Int8Array] BYTES_PER_ELEMENT: 1 },
  402. Uint8Array: { [Function: Uint8Array] BYTES_PER_ELEMENT: 1 },
  403. ...
  404. >global === this
  405. true
  406.  
  407. \test.js
  408. console.log(this); \ {}
  409. console.log(this === global); \ fasle
  410.  
  411. <script type="text/javascript">
  412. foo = "bar";
  413.  
  414. function testThis() {
  415. this.foo = "foo";
  416. }
  417.  
  418. console.log(this.foo); //logs "bar"
  419. testThis();
  420. console.log(this.foo); //logs "foo"
  421. </script>
  422.  
  423. <script type="text/javascript">
  424. foo = "bar";
  425.  
  426. function testThis() {
  427. "use strict";
  428. this.foo = "foo";
  429. }
  430.  
  431. console.log(this.foo); //logs "bar"
  432. testThis(); //Uncaught TypeError: Cannot set property 'foo' of undefined
  433. </script>
  434.  
  435. <script type="text/javascript">
  436. foo = "bar";
  437.  
  438. function testThis() {
  439. this.foo = "foo";
  440. }
  441.  
  442. console.log(this.foo); //logs "bar"
  443. new testThis();
  444. console.log(this.foo); //logs "bar"
  445.  
  446. console.log(new testThis().foo); //logs "foo"
  447. </script>
  448.  
  449. function Thing() {
  450. console.log(this.foo);
  451. }
  452.  
  453. Thing.prototype.foo = "bar";
  454.  
  455. var thing = new Thing(); //logs "bar"
  456. console.log(thing.foo); //logs "bar"
  457.  
  458. function Thing() {
  459. this.things = [];
  460. }
  461.  
  462. var thing1 = new Thing();
  463. var thing2 = new Thing();
  464. thing1.things.push("foo");
  465. console.log(thing1.things); //logs ["foo"]
  466. console.log(thing2.things); //logs []
  467.  
  468. var obj = {
  469. foo: "bar",
  470. logFoo: function () {
  471. console.log(this.foo);
  472. }
  473. };
  474.  
  475. obj.logFoo(); //logs "bar"
  476.  
  477. function Listener() {
  478. document.getElementById("foo").addEventListener("click",
  479. this.handleClick);
  480. }
  481. Listener.prototype.handleClick = function (event) {
  482. console.log(this); //logs "<div id="foo"></div>"
  483. }
  484.  
  485. var listener = new Listener();
  486. document.getElementById("foo").click();
  487.  
  488. function Listener() {
  489. document.getElementById("foo").addEventListener("click",
  490. this.handleClick.bind(this));
  491. }
  492. Listener.prototype.handleClick = function (event) {
  493. console.log(this); //logs Listener {handleClick: function}
  494. }
  495.  
  496. var listener = new Listener();
  497. document.getElementById("foo").click();
  498.  
  499. <div id="foo" onclick="console.log(this);"></div>
  500. <script type="text/javascript">
  501. document.getElementById("foo").click(); //logs <div id="foo"...
  502. </script>
  503.  
  504. function Thing () {
  505. }
  506. Thing.prototype.foo = "bar";
  507. Thing.prototype.logFoo = function () {
  508. eval("console.log(this.foo)"); //logs "bar"
  509. }
  510.  
  511. var thing = new Thing();
  512. thing.logFoo();
  513.  
  514. function Thing () {
  515. }
  516. Thing.prototype.foo = "bar";
  517. Thing.prototype.logFoo = function () {
  518. with (this) {
  519. console.log(foo);
  520. foo = "foo";
  521. }
  522. }
  523.  
  524. var thing = new Thing();
  525. thing.logFoo(); // logs "bar"
  526. console.log(thing.foo); // logs "foo"
  527.  
  528. <div class="foo bar1"></div>
  529. <div class="foo bar2"></div>
  530. <script type="text/javascript">
  531. $(".foo").each(function () {
  532. console.log(this); //logs <div class="foo...
  533. });
  534. $(".foo").on("click", function () {
  535. console.log(this); //logs <div class="foo...
  536. });
  537. $(".foo").each(function () {
  538. this.click();
  539. });
  540. </script>
  541.  
  542. var val = "window.val"
  543.  
  544. var obj = {
  545. val: "obj.val",
  546. innerMethod: function () {
  547. var val = "obj.val.inner",
  548. func = function () {
  549. var self = this;
  550. return self.val;
  551. };
  552.  
  553. return func;
  554. },
  555. outerMethod: function(){
  556. return this.val;
  557. }
  558. };
  559.  
  560. //This actually gets executed inside window object
  561. console.log(obj.innerMethod()()); //returns window.val
  562.  
  563. //Breakdown in to 2 lines explains this in detail
  564. var _inn = obj.innerMethod();
  565. console.log(_inn()); //returns window.val
  566.  
  567. console.log(obj.outerMethod()); //returns obj.val
  568.  
  569. var status = 1;
  570. var helper = {
  571. status : 2,
  572. getStatus: function () {
  573. return this.status;
  574. }
  575. };
  576.  
  577. var theStatus1 = helper.getStatus(); //line1
  578. console.log(theStatus1); //2
  579.  
  580. var theStatus2 = helper.getStatus;
  581. console.log(theStatus2()); //1
  582.  
  583. function Person(name){
  584. this.personName = name;
  585. this.sayHello = function(){
  586. return "Hello " + this.personName;
  587. }
  588. }
  589.  
  590. var person1 = new Person('Scott');
  591. console.log(person1.sayHello()); //Hello Scott
  592.  
  593. var person2 = new Person('Hugh');
  594. var sayHelloP2 = person2.sayHello;
  595. console.log(sayHelloP2()); //Hello undefined
  596.  
  597. function testFunc() {
  598. this.name = "Name";
  599. this.myCustomAttribute = "Custom Attribute";
  600. return this;
  601. }
  602.  
  603. var whatIsThis = testFunc();
  604. console.log(whatIsThis); //window
  605.  
  606. var whatIsThis2 = new testFunc();
  607. console.log(whatIsThis2); //testFunc() / object
  608.  
  609. console.log(window.myCustomAttribute); //Custom Attribute
  610.  
  611. <script type="application/javascript">
  612. function click_handler() {
  613. alert(this); // alerts the window object
  614. }
  615. </script>
  616.  
  617. <button id='thebutton' onclick='click_handler()'>Click me!</button>
  618.  
  619. <script type="text/javascript" language="javascript">
  620. $('#tbleName tbody tr').each(function{
  621. var txt='';
  622. txt += $(this).find("td").eq(0).text();
  623. \same as above but synatx different
  624. var txt1='';
  625. txt1+=$('#tbleName tbody tr').eq(0).text();
  626. alert(txt1)
  627. });
  628. </script>
  629.  
  630. $('#a').click(function(){
  631. console.log($(this).attr('href'));
  632. });
Add Comment
Please, Sign In to add comment