将方法添加到js中的函数内部的对象 [英] Adding a method to an object that is inside of a function in js

查看:469
本文介绍了将方法添加到js中的函数内部的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是来自我正在努力的挑战。有没有什么办法可以将引入方法添加到 personStore 对象中,而不使用关键字this。任何洞察力都将不胜感激。


使用Object.create

挑战1/3



personStore 对象中,创建一个属性greet,其中的值是一个函数那就是hello。



挑战2/3



创建一个函数 personFromPersonStore 以输入名称和年龄作为输入。 >当被调用时,函数将使用 personStore 对象上的Object.create方法创建person对象。



挑战3/3



如果不编辑已编写的代码,请添加介绍方法添加到记录我的名字是[姓名]的 personStore 对象中。

侧面好奇心



作为一个方面说明,很好奇是否有方法将引入方法添加到位于personFromPersonStore函数内部的person对象。


我的解决方案:

  var personStore = {
//在这里添加代码
greet:function(){
console.log('Hello');
}
};

function personFromPersonStore(name,age){
var person = Object.create(personStore);
person.name = name;
person.age = age;
person.greet = personStore.greet;
return person;
};

personStore.introduce = function(){
console.log('我的名字是'+ this.name)
}

/ /挑战3测试者
sandra.introduce(); // - > Logs'我的名字是Sandra


解决方案

但是使用这个要简单得多。



这段代码传递名字属性作为参数,但由于该属性已经可以通过引入函数作为通过这个,这有点浪费。



var personStore = {//在这里添加代码greet:function(){console.log('Hello'); }}; function personFromPersonStore(name,age){var person = Object.create(personStore); person.name = name; person.age =年龄; person.greet = personStore.greet;返回者; }; personStore.introduce = function(nm){console.log('Hi,my name is'+ nm)} person1 = personFromPersonStore('Fred',21); person1.introduce(person1.name);


Hi this is from a challenge I was working on. Is there any way i can add the introduce method to the personStore object without using the keyword this. Any insight is greatly appreciated.

Using Object.create

Challenge 1/3

Inside personStore object, create a property greet where the value is a function that logs "hello".

Challenge 2/3

Create a function personFromPersonStore that takes as input a name and an age. > When called, the function will create person objects using the Object.create method on the personStore object.

Challenge 3/3

Without editing the code you've already written, add an introduce method to the personStore object that logs "Hi, my name is [name]".

Side Curiosity

As a side note, was curious if there was a way to add the introduce method to the person object that sits inside of the personFromPersonStore function.

my solution:

var personStore = {
    // add code here
  greet: function (){
    console.log('Hello');
  }
};

function personFromPersonStore(name, age) {
  var person = Object.create(personStore);
  person.name = name;
  person.age = age;
  person.greet = personStore.greet;
  return person;    
};

personStore.introduce = function () {
  console.log('Hi, my name is ' + this.name)
}

//Challenge 3 Tester
sandra.introduce(); // -> Logs 'Hi, my name is Sandra

解决方案

You can, but using this is a lot simpler.

This code passes the name property as an argument, but as the property is already accessible to the introduce function as an internal property via this, it is a bit wasteful.

var personStore = {
    // add code here
  greet: function (){
    console.log('Hello');
  }
};

function personFromPersonStore(name, age) {
  var person = Object.create(personStore);
  person.name = name;
  person.age = age;
  person.greet = personStore.greet;
  return person;    
};

personStore.introduce = function (nm) {
  console.log('Hi, my name is ' + nm)
}

person1=personFromPersonStore('Fred',21);
person1.introduce(person1.name);

这篇关于将方法添加到js中的函数内部的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆