最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Not a function? - Stack Overflow

programmeradmin2浏览0评论

When I run the following code I get told, that talk is not a function. Why?

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

cat("felix");
cat.talk()

When I run the following code I get told, that talk is not a function. Why?

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

cat("felix");
cat.talk()
Share Improve this question asked Mar 16, 2012 at 22:03 dublintechdublintech 17.8k31 gold badges88 silver badges118 bronze badges 1
  • Were you trying to make a class cat, or define an object which had a function talk? – Chris Carew Commented Mar 16, 2012 at 22:06
Add a ment  | 

4 Answers 4

Reset to default 9

What you're trying to do is create an object for which the function is a constructor, but what the code is actually doing is setting the variable talk to a function. You want:

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

var myCat = new cat("felix");
myCat.talk()

edit:

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

He talks about constructing objects with functions at about 30 minutes in. The code he posts 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 relevant quote:

The new keyword is just a shorthand that is saying "make a new object and call the constructor on it ... the new keyword has no other meaning"

In other words, he's saying that when using the new keyword, you're defining your variable as an object and calling the function in the context of that object (this points to your object).

The extra thing that the new keyword does is set the prototype of the newly made object to the prototype of the constructor. So if we do:

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 is now true.

To make your code work as desired you would have to write:

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

var c = new Cat("felix");
c.talk()

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

Your original code actually declared a global function talk which wasn't part of the cat function at all, since it was missing the var keyword.

That's simply because it isn't.

You have created a function and assigned to a variable in the cat function, but that variable doesn't belong to the function. As you don't declare the variable anywhere, it implicitly bees global, so it's actually available outside the function, but not the way that you try to use it.

You would have to add the function as a property to the cat function object, in order for you to be able to call it that way:

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

cat("felix");
cat.talk()

However, you might be looking for an object that has a method, rather than 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();

cat is not an object. It's a function, and I don't think JavaScript supports this.

发布评论

评论列表(0)

  1. 暂无评论