简体   繁体   English

如何访问函数的属性?

[英]How to access a property on a function?

I am just starting to learn Javascript and was experimenting with functions. 我刚刚开始学习Javascript,并正在尝试一些函数。 I wrote the following thinking that it would work. 我写了以下想法,认为它会起作用。 Is something like this possible or is it just wrong ? 这样的事情是可能的还是仅仅是错误的

function foo() {
   console.log(this.language);
}

foo.language = "English";

foo();

It is not possible, because to gain access to this as the current object, you'll need to instantiate the (constructor) function, which is what invoking new will do for you. 这是不可能的,因为要访问this作为当前对象,你需要实例化(构造函数)的功能,这是调用new会为你做。 Accessing this for a reference to the current instance is not only the case in Javascript though; 但是,访问this实例以引用当前实例不仅是Javascript中的情况,而且还包括 in Java, this also isn't accessible from static context. 在Java中, this也不能从静态上下文中访问。

What you're looking for is something like: 您正在寻找的是这样的:

function Foo(language) {
   this.language = language; 

}
Foo.prototype.log = function(){
   console.log(this.language); 
}    
var bar = new Foo("English"); 
bar.log(); 

You can make a foo object (an instance of foo ) using the new keyword. 您可以使用一个Foo对象(FOO的一个实例) new关键字。 Then set the language property of that instance. 然后设置该实例的language属性。 If foo has a function to output its language property, then you can invoke that function on your instance to see its language . 如果foo具有输出其language属性的函数,则可以在实例上调用该函数以查看其language

function foo() {
    this.outputLanguage = function() {
        console.log(this.language);
    }
}

var f1 = new foo();
f1.language = "English";

var f2 = new foo();
f2.language = "French";

f1.outputLanguage();
f2.outputLanguage();

In this example, we make 2 foo objects, and assign each a different language. 在此示例中,我们制作了2个foo对象,并为每个对象分配了不同的语言。

The foo() function is a constructor function . foo()函数是一个构造函数 We usually pass the initial values of the instance variables to it, like this: 我们通常将实例变量的初始值传递给它,如下所示:

function foo(language) {
    this.language = language;
    this.outputLanguage = function() {
        console.log(this.language);
    }
}

var f1 = new foo("English");
var f2 = new foo("French");

f1.outputLanguage();
f2.outputLanguage();

Javascript only supports prototypal Inheritance. Javascript仅支持原型继承。 So to assign a property (in your case language is the property) to an Object (in your case foo ) you need to use constructor. 因此,要将属性(在您的情况下为language是分配给对象(在您的情况下为foo ),则需要使用构造函数。 And by convention constructor are written Initcap. 按照约定,构造函数是Initcap编写的。

function Foo(lang){
 this.language = lang;
}

Foo.prototype.getLanguage = function(){
  return this.language;
}

var lang1 = new Foo('english');
var lang2 = new Foo('bengali');

console.log(lang1.getLanguage()); // english
console.log(lang2.getLanguage()); // bengali

this is available on instance variables, in your case it's named function this可以用实例变量,在你的情况下,它的命名功能

function foo() {
   console.log(foo.language); //instead of this, use function property
}

foo.language = "English";

foo();

If you want to dive more, checkout my other answer for the same topic 如果您想进一步潜水,请查看我针对同一主题的其他答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM