Not a function?

When I run the following code, they tell me that talking is not a function. Why?

function cat(name) { talk = function() { alert(" say meeow!" ) } } cat("felix"); cat.talk() 
+6
source share
4 answers

What you are trying to do is create an object for which the function is the constructor, but what the code actually does is set the talk variable to the function. Do you want to:

 function cat(name) { this.talk = function() { alert(" say meeow!" ) } } var myCat = new cat("felix"); myCat.talk() 

edit:

Corresponding javascript tech talk: http://www.youtube.com/watch?v=ljNi8nS5TtQ

He talks about building objects with features in about 30 minutes. The code he publishes is:

 function Circle(radius){ this.radius = radius; this.area = function(){ return this.radius * this.radius * Math.PI; }; } var instance = {}; Circle.call(instance, 5); instance.area(); // ==> 78.5398 var instance2 = new Circle(5); instance2.area() // ==> 78.5398 instance instanceof Circle // ==> false instance2 instanceof Circle // ==> true 

And the corresponding quote:

A new keyword is simply an abbreviation that says "make a new object and call it a constructor ... the new keyword has no other meaning"

In other words, he says that when you use the new keyword, you define your variable as an object and call a function in the context of this object ( this points to your object).

In addition, the new keyword specifies the prototype of the newly created object for the prototype constructor. So, if we do this:

 function Circle(radius){ this.radius = radius; this.area = function(){ return this.radius * this.radius * Math.PI; }; } var instance = {}; Circle.call(instance, 5); instance.__proto__ = Circle.prototype; // we set the prototype of the new object to that of the constructor instance.area(); // ==> 78.5398 var instance2 = new Circle(5); instance2.area() // ==> 78.5398 instance instanceof Circle // ==> true // this is now true instance2 instanceof Circle // ==> true 

instance instanceof Circle now true.

+9
source

For your code to work as desired, you will need to write:

 function Cat(name) { this.talk = function() { alert(" say meeow!" ) } }; var c = new Cat("felix"); c.talk() 

The Cat function is a constructor function, and the returned object has a ( talk ) property, which is a function that you can call.

In the source code, the global talk function is actually declared, which was not part of the Cat function at all, since it did not contain the var keyword.

+2
source

This is simply because it is not.

You created a function and assigned a variable in the cat function, but this variable does not belong to the function. Since you do not declare a variable anywhere, it becomes implicitly global, so it is actually available outside the function, but not the way you are trying to use it.

You need to add the function as a property to the cat function object so that you can call it that way:

 function cat(name) { cat.talk = function() { alert(" say meeow!" ) } } cat("felix"); cat.talk() 

However, you can search for an object that has a method, and not a function that has a property that is a method:

 function Cat(name) { this.name = name; } Cat.prototype.talk = function() { alert(this.name + " says meeow!"); } var felix = new Cat("Felix"); felix.talk(); 
+1
source

cat is not an object. This is a feature, and I don't think JavaScript supports this.

0
source

Source: https://habr.com/ru/post/910953/


All Articles