Something is wrong with code

Hi,Iam trying to execute this code but its showing some error kindly help

var cat={
sound:"miaow",
name:"kitty",
speak:speak


};
var speak=function(){
  console.log(`${this.sound} my name is ${this.name}`);
}
cat.speak();

Can you write what error? If this is from one of the challenges can you link to it? It will be easier to help with this information.

[stdin]:13
cat.speak();
    ^

TypeError: cat.speak is not a function
    at [stdin]:13:5
    at Script.runInThisContext (vm.js:120:20)
    at Object.runInThisContext (vm.js:311:38)
    at Object.<anonymous> ([stdin]-wrapper:10:26)
    at Module._compile (internal/modules/cjs/loader.js:1156:30)
    at evalScript (internal/process/execution.js:94:25)
    at internal/main/eval_stdin.js:29:5
    at Socket.<anonymous> (internal/process/execution.js:206:5)
    at Socket.emit (events.js:322:22)
    at endReadableNT (_stream_readable.js:1187:12)
[Finished in 1.38s]

Hi, inside the cat object you should be writing the speak function because you are calling the instances of cat’s name and sound.

Hi,welcome,but iam using this keyword i.e i will going to use this function on other objects as well. so i dont think i should write it inside my object.

var cat = {
  sound: “miaow”,
  name: “kitty”,
};
cat.speak = function(){
  console.log(`${this.sound}, my name is ${this.name}`);
}
cat.speak();

create a new function and assign it to a key outside of object

in that case you could use call method for other object.

var cat={
  sound:"miaow",
  name:"kitty",
  speak: function(){
    console.log(this.sound + ' my name is ' + this.name);
    }  
  };
  cat.speak()  //miaow my name is kitty 
let dog = {
  sound : 'woof',
  name : 'champ'
}
  cat.speak.call(dog) //woof my name is champ

cant we use speak function as a method in other objects??

perhaps you can use object constructor and create cat object instead?

let Animal = function(name,sound){
  this.name = name
  this.sound = sound
}

Animal.prototype.speak = function(){
  console.log(this.sound + ' my name is ' + this.name);
}

const cat = new Animal('kitty', 'miaow')
cat.speak()

const dog = new Animal('woof', 'champ')
dog.speak()

actually iam still learning constructor but thanks all of your solutions are working but .what iam trying to achieve is using function as a method in different objects and a simple call to that object like cat.speak() ,dog.speak i dont know why its saying cat.speak is not a function.

are you looking for something like this?

let cat = {
    name : 'kitty',
    sound : 'miaow'
}

function speak(obj){
  console.log(obj.sound + ' my name is ' + obj.name)
}
speak(cat) // miaow my name is kitty 
var cat={
sound:"miaow",
name:"kitty",
speak:speak
};

var dog={
  sound:"voof",
  Name:"sweety",
  speak: speak
};

var speak=function () {
  console.log(`${this.sound} my name is ${this.name}`);
}
cat.speak();
dog.speak();

something that let worth using “this” keyword

try this.

let cat = {
    name : 'kitty',
    sound : 'miaow',
    speak : speak
}

let dog = {
  name : 'sweety',
  sound : 'voof',
  speak : speak
}

function speak(){
  console.log(this.sound + ' my name is ' + this.name)
}

cat.speak()
dog.speak()

Yes this is working please tell me what i was doing wrong? and no semicolon while calling function?

Because you are using the function expression for your speak method.
Function expressions in JavaScript are not hoisted, unlike function declarations . You can’t use function expressions before you define them.

there is one more problem can i share??

@Gurpreet_Singh @khun.7

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

Please use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks are not single quotes.

markdown_Forums

Hi, i’ll remember this from next time,Thanks