不是功能? [英] Not a function?
问题描述
当我运行下面的代码时,我会被告知,这种谈话不是一种功能。
为什么?
函数cat(name){
talk = function(){
alert(say meeow!)
}
}
cat(felix);
cat.talk()
'试图做的是创建一个对象,其中的函数是一个构造函数,但代码实际上在做的是将变量 talk
设置为一个函数。你需要:
$ b $ pre $ 函数cat(name){
this.talk = function(){
alert (say meeow!)
}
}
var myCat =新猫(felix);
myCat.talk()
编辑:
相关javascript技术讨论: http://www.youtube.com/watch?v = ljNi8nS5TtQ
他谈到了用约30分钟的时间构建函数的对象。他发布的代码是:
函数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 // ==>真
以及相关的引用:
new关键字只是一个简写,即创建一个新对象
并在其上调用构造函数... new关键字没有其他
含义
换句话说,当他使用 new
关键字时,您正在定义你的变量作为一个对象,并在该对象的上下文中调用该函数( this
指向你的对象)。
new
关键字所做的额外事情是将新创建的对象的原型设置为构造函数的原型。因此,如果我们这样做:
pre $ 函数Circle(radius){
this.radius = radius;
this.area = function(){
return this.radius * this.radius * Math.PI;
};
}
var instance = {};
Circle.call(instance,5);
实例.__ proto__ = Circle.prototype; //我们将新对象的原型设置为构造函数的原型
instance.area(); // ==> 78.5398
var instance2 = new Circle(5);
instance2.area()// ==> 78.5398
instance instanceof Circle // ==> true //现在是真的
instance2 instanceof Circle // ==> true
实例instanceof Circle
现在成立。
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()
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.com/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.
这篇关于不是功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!