0

How can I avoid retyping the same function in my team members? The say_name function can be used by all members in team obj. How to write my code?

<script type="text/javascript">
var team={
  // member
  jimmy:{
    name:'jimmy',
    say_name:function (){}
  },
  darcy:{
    name:'darcy',
    say_name:function(){}
  },
  // common functions
  say_name: function(){
    // every member has the say_name function, I don't want to write the same code in each member. How to do?
    alert('my name is ??')
  }
}
</script>
dda
  • 6,030
  • 2
  • 25
  • 34
mingfish_004
  • 1,385
  • 2
  • 18
  • 26
  • 3
    http://stackoverflow.com/questions/1595611/how-to-properly-create-a-custom-object-in-javascript#1598077 – zerkms May 09 '13 at 01:56
  • Some more on object cration using functions in JavaScript: http://stackoverflow.com/questions/16063394/prototypical-inheritance-writing-up/16063711#16063711 – HMR May 09 '13 at 02:24

3 Answers3

2

Use bind:

var say_name = function () {
    alert("my name is " + this.name);
}

var jimmy = {
    name: 'jimmy',
    say_name: say_name.bind(jimmy)
};

jimmy.say_name(); // my name is jimmy

There are probably much more straightforward ways to do it though.

Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
Halcyon
  • 57,230
  • 10
  • 89
  • 128
  • 1
    "There are probably much more straightforward ways to do it though." ...like not using `bind` ;) `say_name: say_name` would suffice (`this` already refers to whatever object the function is a property of, in this case, `jimmy`). – Dagg Nabbit May 09 '13 at 02:18
1

you can use prototype, first define a construtcor function:

function Team(){

}

Team.prototype.say_name=function(){
     // every team will have this method
}

construct a team:

var team=new Team();//now the team has a method called say_name
team.jimmy={name:'jimmy'}

updated:
here's a great article about javascript common pattern, it's very worth reading:) http://addyosmani.com/resources/essentialjsdesignpatterns/book/

ltebean
  • 1,019
  • 6
  • 15
  • here's a great article about javascript common pattern, it's very worth reading:) http://addyosmani.com/resources/essentialjsdesignpatterns/book/ – ltebean May 09 '13 at 02:13
1

It took a little longer to formulate this answer. I think you want to have multiple team instances with multipl player instances that have variable names and such. If that's the case then hard coding a jimmy member isn't the way to go.

// object defenition
function Team(){
  // property unuque for every team so teamB and teamC
  // would not have the same members
  this.members=[];
}
// properties shared by all team instances so teamB and teamC 
//   would refer to the same function
Team.prototype.addMember=function(name){
  this.members.push(new TeamMember(name));
}
Team.prototype.getMember=function(name){
  for(var i=0;i<this.members.length;i++){
    if(this.members[i].name===name){
      return this.members[i];
    }
  }
}


// Define a TeamMember, this is the definition, Bob would be
//   and instance o TeamMember; like var Bob=new TeamMember("Bob");
function TeamMember(name){
  this.name=name;
}
// properties shared by all TeamMember instance objects
//   so bill and jane will point to this function
TeamMember.prototype.sayName=function(){
  console.log("My name is:" + this.name);
}
// end of object definition

//creating instances
var teamB=new Team();
var teamC=new Team();
teamB.addMember("Tom");
teamB.addMember("Harry");
teamC.addMember("July");
teamC.addMember("Mary");
teamC.getMember("July").sayName();
HMR
  • 37,593
  • 24
  • 91
  • 160
  • You're welcome, if you're going to use OO in JS please read and try to understand the link with the Hamster object. – HMR May 09 '13 at 02:59