Javascript中的子函数
需要在JS中编写代码的建议。这是我的密码:Javascript中的子函数,javascript,Javascript,需要在JS中编写代码的建议。这是我的密码: function Book(name) { this.name = name; // Some vars need to be downloaded $.ajax({ method: "POST", url: "http://localhost/books.php" }).done(function( res ) { this.price = this.analyze(re
function Book(name) {
this.name = name;
// Some vars need to be downloaded
$.ajax({
method: "POST",
url: "http://localhost/books.php"
}).done(function( res ) {
this.price = this.analyze(res); // <--WRONG
return this;
});
}
Book.prototype.analyze = function(res){
var output = 50 * res;
// do something
return output;
}
功能手册(名称){
this.name=名称;
//需要下载一些VAR
$.ajax({
方法:“张贴”,
url:“http://localhost/books.php"
}).完成(功能(res){
this.price=this.analyze(res);//在$.ajax
行之前,放置var=this;
。然后在$行之前更改。ajax
行,放置var=this;
。然后在$行之前更改。ajax
行,放置var=this;
。然后在$行之前更改。ajax
行,放置varthat=this;
。然后更改$.ajax()的,其中有一个参数“context”。您可以使用它。因此,您的代码将
function Book(name) {
this.name = name;
// Some vars need to be downloaded
$.ajax({
context: this,
method: "POST",
url: "http://localhost/books.php"
}).done(function( res ) {
this.price = this.analyze(res); // <-- not wrong any more
return this;
});
}
功能手册(名称){
this.name=名称;
//需要下载一些VAR
$.ajax({
背景:这,,
方法:“张贴”,
url:“http://localhost/books.php"
}).完成(功能(res){
this.price=this.analyze(res);//对于$.ajax()有一个参数“context”。您可以使用它。因此,您的代码将是
function Book(name) {
this.name = name;
// Some vars need to be downloaded
$.ajax({
context: this,
method: "POST",
url: "http://localhost/books.php"
}).done(function( res ) {
this.price = this.analyze(res); // <-- not wrong any more
return this;
});
}
功能手册(名称){
this.name=名称;
//需要下载一些VAR
$.ajax({
背景:这,,
方法:“张贴”,
url:“http://localhost/books.php"
}).完成(功能(res){
this.price=this.analyze(res);//对于$.ajax()有一个参数“context”。您可以使用它。因此,您的代码将是
function Book(name) {
this.name = name;
// Some vars need to be downloaded
$.ajax({
context: this,
method: "POST",
url: "http://localhost/books.php"
}).done(function( res ) {
this.price = this.analyze(res); // <-- not wrong any more
return this;
});
}
功能手册(名称){
this.name=名称;
//需要下载一些VAR
$.ajax({
背景:这,,
方法:“张贴”,
url:“http://localhost/books.php"
}).完成(功能(res){
this.price=this.analyze(res);//对于$.ajax()有一个参数“context”。您可以使用它。因此,您的代码将是
function Book(name) {
this.name = name;
// Some vars need to be downloaded
$.ajax({
context: this,
method: "POST",
url: "http://localhost/books.php"
}).done(function( res ) {
this.price = this.analyze(res); // <-- not wrong any more
return this;
});
}
功能手册(名称){
this.name=名称;
//需要下载一些VAR
$.ajax({
背景:这,,
方法:“张贴”,
url:“http://localhost/books.php"
}).完成(功能(res){
this.price=this.analyze(res);//即使这样,它也不会起作用,因为此
在错误的行中与函数相关。此外,根据GetPrice中的操作是否需要很长时间,您可能应该使用回调,而不是将变量设置为函数的结果。您可以在构造函数的顶部添加var myBook=this;
,然后运行t使用myBook.price=myBook.getPrice()
,因为myBook将是范围的一部分,但是此
会随着范围的变化而变化。(在原型中仍然需要getPrice
)即使重复,它也不会起作用,因为此
在错误的行上与函数有关。此外,根据GetPrice中的操作是否需要很长时间,您可能应该使用回调,而不是将变量设置为函数的结果。您可以在构造函数的顶部添加var myBook=this;
,然后使用myBook.price=myBook.getPrice()
运行该行,因为myBook将是作用域的一部分,但是this
会随着作用域的变化而变化。(在原型中仍然需要getPrice
)即使重复,它也不会起作用,因为此
在错误的行上与函数有关。此外,根据GetPrice中的操作是否需要很长时间,您可能应该使用回调,而不是将变量设置为函数的结果。您可以在构造函数的顶部添加var myBook=this;
,然后使用myBook.price=myBook.getPrice()
运行该行,因为myBook将是作用域的一部分,但是this
会随着作用域的变化而变化。(在原型中仍然需要getPrice
)即使重复,它也不会起作用,因为此
在错误的行上与函数有关。此外,根据GetPrice中的操作是否需要很长时间,您可能应该使用回调,而不是将变量设置为函数的结果。您可以在构造函数的顶部添加var myBook=this;
,然后使用myBook.price=myBook.getPrice()
运行该行,因为myBook将是作用域的一部分,但是this
会随着作用域的变化而变化。(在原型中仍然需要getPrice
)的副本我不知怎的认为还有另一种方法可以做到这一点,除了在“父”中将“this”定义为变量之外函数。但是,我想没有。谢谢!我不知怎么想,除了在“父”函数中将“this”定义为一个变量之外,还有其他的方法。但是,我想没有。谢谢!我不知怎么想,除了在“父”函数中将“this”定义为一个变量之外,还有其他的方法函数。但是,我想没有。谢谢!我不知怎么想,除了将“this”定义为“parent”函数中的变量外,还有另一种方法。但是,我想没有。谢谢!