1、 阅读代码

  1. function foo() {
  2. console.log( this.a );
  3. }
  4. function doFoo() {
  5. foo();
  6. }
  7. var obj = {
  8. a: 1,
  9. doFoo: doFoo
  10. };
  11. var a = 2;
  12. obj.doFoo()

2、 阅读代码

  1. var a = 10
  2. var obj = {
  3. a: 20,
  4. say: () => {
  5. console.log(this.a)
  6. }
  7. }
  8. obj.say()
  9. var anotherObj = { a: 30 }
  10. obj.say.apply(anotherObj)

3、 阅读代码

  1. function a() {
  2. console.log(this);
  3. }
  4. a.call(null);

4、 阅读代码

  1. var obj = {
  2. say: function() {
  3. var f1 = () => {
  4. console.log("1111", this);
  5. }
  6. f1();
  7. },
  8. pro: {
  9. getPro:() => {
  10. console.log(this);
  11. }
  12. }
  13. }
  14. var o = obj.say;
  15. o();
  16. obj.say();
  17. obj.pro.getPro();

5、 阅读代码

  1. var myObject = {
  2. foo: "bar",
  3. func: function() {
  4. var self = this;
  5. console.log(this.foo);
  6. console.log(self.foo);
  7. (function() {
  8. console.log(this.foo);
  9. console.log(self.foo);
  10. }());
  11. }
  12. };
  13. myObject.func();

6、 阅读代码

  1. window.number = 2;
  2. var obj = {
  3. number: 3,
  4. db1: (function(){
  5. console.log(this);
  6. this.number *= 4;
  7. return function(){
  8. console.log(this);
  9. this.number *= 5;
  10. }
  11. })()
  12. }
  13. var db1 = obj.db1;
  14. db1();
  15. obj.db1();
  16. console.log(obj.number);
  17. console.log(window.number);

7、 阅读代码

  1. var length = 10;
  2. function fn() {
  3. console.log(this.length);
  4. }
  5. var obj = {
  6. length: 5,
  7. method: function(fn) {
  8. fn();
  9. arguments[0]();
  10. }
  11. };
  12. obj.method(fn, 1);

8、 阅读代码

  1. var a = 1;
  2. function printA(){
  3. console.log(this.a);
  4. }
  5. var obj={
  6. a:2,
  7. foo:printA,
  8. bar:function(){
  9. printA();
  10. }
  11. }
  12. obj.foo();
  13. obj.bar();
  14. var foo = obj.foo;
  15. foo();

9、 阅读代码

  1. var x = 3;
  2. var y = 4;
  3. var obj = {
  4. x: 1,
  5. y: 6,
  6. getX: function() {
  7. var x = 5;
  8. return function() {
  9. return this.x;
  10. }();
  11. },
  12. getY: function() {
  13. var y = 7;
  14. return this.y;
  15. }
  16. }
  17. console.log(obj.getX())
  18. console.log(obj.getY())

10、 阅读代码

  1. var a = 10;
  2. var obt = {
  3. a: 20,
  4. fn: function(){
  5. var a = 30;
  6. console.log(this.a)
  7. }
  8. }
  9. obt.fn();
  10. obt.fn.call();
  11. (obt.fn)();

11、 阅读代码

  1. function a(xx){
  2. this.x = xx;
  3. return this
  4. };
  5. var x = a(5);
  6. var y = a(6);
  7. console.log(x.x)
  8. console.log(y.x)

12、 阅读代码

  1. function foo(something){
  2. this.a = something
  3. }
  4. var obj1 = {
  5. foo: foo
  6. }
  7. var obj2 = {}
  8. obj1.foo(2);
  9. console.log(obj1.a);
  10. obj1.foo.call(obj2, 3);
  11. console.log(obj2.a);
  12. var bar = new obj1.foo(4)
  13. console.log(obj1.a);
  14. console.log(bar.a);

13、 阅读代码

  1. function foo(something){
  2. this.a = something
  3. }
  4. var obj1 = {}
  5. var bar = foo.bind(obj1);
  6. bar(2);
  7. console.log(obj1.a);
  8. var baz = new bar(3);
  9. console.log(obj1.a);
  10. console.log(baz.a);