用两种以上的 方式实现一个方法或者对象,调用时打印"你好xx",已定义的代码不能做修改,自己编译的不能出现"你好"? (Javasctript)

先上代码

const obj = {
  say(){
    Array.from(arguments).forEach(item=>{
      console.log(`${this.str} ${item}`)
    })
  }
}
Object.defineProperties(obj, {
    'str':{
      value:'hello',
         writable:false
     }
});
            
var obj1={};
            
Object.defineProperties(obj1, {
     'str':{
         value:'你好',
         writable:false
      }
});
 
/*方法1*/ function fo() { obj.say.call(obj1,...arguments); } fo('foo'); fo('foo','bar'); /*方法2*/ const f1=obj.say.bind(obj1); f1('foo'); f1('foo','bar'); /*f方法3*/ function Person(){ this.str=obj1.str; this.say=obj.say; } const c=new Person();     c.say("foo");     c.say("foo",'bar');

以上为朋友给看的一道题,然而自己又是菜鸟,也不知道写的符不符合,欢迎来喷,不在喷中成长,就在喷中死亡!

posted @ 2019-12-04 10:27  冷落清秋  阅读(363)  评论(0编辑  收藏  举报