例如
var contents = 'some text';
function fileSave(path){
// I'll handle saving the file, but I need the file contents
}
contents.fileSave('index.html');
因此,当函数对 contents 变量进行 fun 时,函数可以访问该变量。就像 replace() 在 JavaScript 中的工作方式一样。
例如。
str.replace();
但在这种情况下
contents.fileSave();
变量是可以互换的,适用于任何变量的函数也是附加的。
对不起,新手..
最佳答案
这可能是不修改 String.prototype
的更好方法
function fileEditor(path) {
this.save = function(data) {
// do something with path & data
}
}
用法是,
var someFile = new fileEditor("index.html");
someFile.save("some text");
关于javascript - 将函数应用于变量? JavaScript/jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13664370/