从另一个类内部调用方法

时间:2015-06-30 04:36:04

标签: javascript node.js

我有一个这样的节点类:

var Classes = function () {
};

Classes.prototype.methodOne = function () {
    //do something
};

当我想打电话给methodOne时,我用这个:

this. methodOne();

它有效。但是现在我必须从另一个类的另一个方法中调用它。这次它不起作用,无法访问methodOne

var mongoose = new Mongoose();
mongoose.save(function (err, coll) {
  //save to database
  this. methodOne(); //this does not work
}

我怎么能调用methodOne?我使用Classes.methodOne()但它不起作用

3 个答案:

答案 0 :(得分:5)

this回调中的

save位于新上下文中,与外部this不同。methodOne在可以访问var that = this; mongoose.save(function (err, coll) { //save to database that.methodOne(); }

的变量中保留
<build>
    <extensions>
        <!-- Enabling the use of Wagon file -->
        <extension>
            <groupId>org.apache.maven.wagon</groupId>
            <artifactId>wagon-file</artifactId>
            <version>1.0-beta-6</version>
        </extension>
    </extensions>

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-deploy-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <skip>true</skip>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>wagon-maven-plugin</artifactId>
            <version>1.0-beta-3</version>
            <executions>
                <execution>
                    <id>upload-jar-to-folder</id>
                    <phase>deploy</phase>
                    <goals>
                        <goal>upload</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <fromDir>${project.build.directory}/classes/public</fromDir>
                <includes>**/*</includes>
                <url>file:/home/lukasz/www</url>
                <toDir>Main</toDir>
            </configuration>
        </plugin>
    </plugins>

</build>

答案 1 :(得分:2)

你需要在mongoose函数之外创建变量。

var mongoose = new Mongoose();
mongoose.save((err, col) => {
    //save to database
    this.methodOne();
})

如果您所处的环境适用于ES6,则可以使用 Arrow expression

<script>
    $.getJSON('https://example.com/api/products/GetProductList/', function(data) {
        var output = '<div class="panel panel-default">';
        for (var i in data.Categories) {
            output += '<div class="panel-heading '+data.Categories[i].category_id +'"><a data-toggle="collapse" data-parent="#accordion" href="#' + data.Categories[i].category_id + '-products">';
            output += data.Categories[i].Category + '</a></div>';
            output += '<div id="' + data.Categories[i].category_id + '-products" class="panel-collapse collapse"><div class="panel-body">';
            for (var j in data.Categories[i].Products) {
                output += '<li><a class="ProdLink" href="https://example.com/api/products/GetProductDetail/'+data.Categories[i].Products[j].product_id+'">'+data.Categories[i].Products[j].short_description + " -- " + data.Categories[i].Products[j].coins+' coins</a></li>';
            }
            output += "</div></div>";
        }
        output += "</div>";

        document.getElementById("accordion").innerHTML = output;
    });
</script>

答案 2 :(得分:2)

您可以做的是在其他类中创建类Classes的对象,并使用此对象引用该方法:例如:

var objClasses;
var mongoose = new Mongoose();
mongoose.save(function (err, coll) {
  //save to database
objClasses = new Classes();
  objClasses. methodOne(); //this should work
}

如果不起作用,请说明您想要达到的目标。