The Object class has both methods and functions meaning they both are accessed through Object.nameOfMethodOrFunction(). The following question What is the difference between a method and a function explains the difference between a method and and a function, but it doesn't explain how to create them within an object. For example, the code below defines the method sayHi. But how do you define a function inside the same object?
Object类具有方法和函数,这意味着它们都可以通过Object.nameOfMethodOrFunction()访问。下面的问题方法和函数之间的区别是什么解释了方法和函数之间的区别,但它没有解释如何在对象中创建它们。例如,下面的代码定义了方法sayHi。但是如何在同一个对象中定义一个函数呢?
var johnDoe =
{
fName : 'John',
lName: 'Doe',
sayHi: function()
{
return 'Hi There';
}
};
3 个解决方案
#1
7
The following defines two classes, ClassA
and ClassB
, with equal functionality but different in nature:
以下定义了两个类,ClassA和ClassB,具有相同的功能但性质不同:
function ClassA(name){
this.name = name;
// Defines method ClassA.say in a particular instance of ClassA
this.say = function(){
return "Hi, I am " + this.name;
}
}
function ClassB(name){
this.name = name;
}
// Defines method ClassB.say in the prototype of ClassB
ClassB.prototype.say = function(){
return "Hi, I am " + this.name;
}
As shown below, they doesn't differ much in usage, and they are both "methods".
如下所示,它们在使用上没有太大差别,它们都是“方法”。
var a = new ClassA("Alex");
alert(a.say());
var b = new ClassB("John");
alert(b.say());
So now what you mean for "function", according to the msdn link that you gave as a comment, seems that "function" is just a "static method" like in C# or Java?
那么现在你对“函数”的意思是什么,根据你作为评论给出的msdn链接,似乎“函数”只是像C#或Java中的“静态方法”?
// So here is a "static method", or "function"?
ClassA.createWithRandomName = function(){
return new ClassA("RandomName"); // Obviously not random, but just pretend it is.
}
var a2 = ClassA.createWithRandomName(); // Calling a "function"?
alert(a2.say()); // OK here we are still calling a method.
So this is what you have in your question:
这就是你在问题中的含义:
var johnDoe =
{
fName : 'John',
lName: 'Doe',
sayHi: function()
{
return 'Hi There';
}
};
OK, this is an Object
, but obviously not a class.
好的,这是一个Object,但显然不是一个类。
#2
0
Quoting Aaron with "A method is on an object. A function is independent of an object".
引用Aaron的方法是“一个方法在一个对象上。一个函数独立于一个对象”。
Logically a method is useless without a "this" defined.
逻辑上,如果没有定义“this”,方法就没用了。
Consider this example:
考虑这个例子:
var johnDoe =
{
fName: 'John',
lName: 'Doe',
sayHi: function () {
return 'Hi There, my name is ' + this.fName;
}
};
function sayHi2() {
return 'Hi There, my last name is ' + this.lName;
}
//Will print Hi there, my first name is John
alert(johnDoe.sayHi());
//An undefined will be seen since there is no defined "this" in SayHi2.
alert(sayHi2());
//Call it properly now, using the oject johnDoe for the "this"
//Will print Hi there, my last name is Doe.
alert(sayHi2.call(johnDoe));
#3
0
var johnDoe = {
fName: 'John',
lName: 'Doe',
sayHi: function(){
function message(){ return 'Hi there'; }
return message();
}
};
That's about as good as you're going to get with the object declaration method of creating a 'class' in JavaScript. Just keep in mind that function is only valid within sayHi
's scope.
这与您在JavaScript中创建“类”的对象声明方法一样好。请记住,该功能仅在sayHi的范围内有效。
However, if you use a function as a class structure, you have a little more flexibility:
但是,如果您将函数用作类结构,则可以获得更多灵活性:
var johnDoe = function(){
this.publicFunction = function(){
};
var privateFunction = function(){
};
};
var jd = new johnDoe();
jd.publicFunction(); // accessible
jd.privateFunction(); // inaccessible
(though both are really considered methods since they have access to the object's scope within).
(尽管两者都被认为是方法,因为它们可以访问对象的范围)。
#1
7
The following defines two classes, ClassA
and ClassB
, with equal functionality but different in nature:
以下定义了两个类,ClassA和ClassB,具有相同的功能但性质不同:
function ClassA(name){
this.name = name;
// Defines method ClassA.say in a particular instance of ClassA
this.say = function(){
return "Hi, I am " + this.name;
}
}
function ClassB(name){
this.name = name;
}
// Defines method ClassB.say in the prototype of ClassB
ClassB.prototype.say = function(){
return "Hi, I am " + this.name;
}
As shown below, they doesn't differ much in usage, and they are both "methods".
如下所示,它们在使用上没有太大差别,它们都是“方法”。
var a = new ClassA("Alex");
alert(a.say());
var b = new ClassB("John");
alert(b.say());
So now what you mean for "function", according to the msdn link that you gave as a comment, seems that "function" is just a "static method" like in C# or Java?
那么现在你对“函数”的意思是什么,根据你作为评论给出的msdn链接,似乎“函数”只是像C#或Java中的“静态方法”?
// So here is a "static method", or "function"?
ClassA.createWithRandomName = function(){
return new ClassA("RandomName"); // Obviously not random, but just pretend it is.
}
var a2 = ClassA.createWithRandomName(); // Calling a "function"?
alert(a2.say()); // OK here we are still calling a method.
So this is what you have in your question:
这就是你在问题中的含义:
var johnDoe =
{
fName : 'John',
lName: 'Doe',
sayHi: function()
{
return 'Hi There';
}
};
OK, this is an Object
, but obviously not a class.
好的,这是一个Object,但显然不是一个类。
#2
0
Quoting Aaron with "A method is on an object. A function is independent of an object".
引用Aaron的方法是“一个方法在一个对象上。一个函数独立于一个对象”。
Logically a method is useless without a "this" defined.
逻辑上,如果没有定义“this”,方法就没用了。
Consider this example:
考虑这个例子:
var johnDoe =
{
fName: 'John',
lName: 'Doe',
sayHi: function () {
return 'Hi There, my name is ' + this.fName;
}
};
function sayHi2() {
return 'Hi There, my last name is ' + this.lName;
}
//Will print Hi there, my first name is John
alert(johnDoe.sayHi());
//An undefined will be seen since there is no defined "this" in SayHi2.
alert(sayHi2());
//Call it properly now, using the oject johnDoe for the "this"
//Will print Hi there, my last name is Doe.
alert(sayHi2.call(johnDoe));
#3
0
var johnDoe = {
fName: 'John',
lName: 'Doe',
sayHi: function(){
function message(){ return 'Hi there'; }
return message();
}
};
That's about as good as you're going to get with the object declaration method of creating a 'class' in JavaScript. Just keep in mind that function is only valid within sayHi
's scope.
这与您在JavaScript中创建“类”的对象声明方法一样好。请记住,该功能仅在sayHi的范围内有效。
However, if you use a function as a class structure, you have a little more flexibility:
但是,如果您将函数用作类结构,则可以获得更多灵活性:
var johnDoe = function(){
this.publicFunction = function(){
};
var privateFunction = function(){
};
};
var jd = new johnDoe();
jd.publicFunction(); // accessible
jd.privateFunction(); // inaccessible
(though both are really considered methods since they have access to the object's scope within).
(尽管两者都被认为是方法,因为它们可以访问对象的范围)。