简体   繁体   中英

Can i add a object inside a method in JavaScript?

var obj = {
  someFunc : function() {
    console.log("Hello");
    console.log(this);
    var obj1 = {
      someFunc1 : function() {
          console.log("Hi");
      }
    };
  }
};

obj.someFunc();
obj.obj1.someFunc1();

I have just created an object. And inside an object, a method, and then a new object and obj1 . I am just trying to check 'this' keyword. But, it seem to me it's grammatical error in js. That is not possible. Right? But, why?

var obj = {
 someFunc : function() {
   console.log("Hello");
   console.log(this);
   return {
      someFunc1 : function() {
      console.log("Hi");
   }
  };
}
};


obj.someFunc().someFunc1();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM