Ron*_*den 3 html javascript jquery jquery-plugins
我想创建一个函数在我的控制台中记录一个字符串.以下只是展示我想要的一个例子.
var helloWorld = 'Hello World';
helloWorld.log();
function log(string)
{
console.log(string);
}
Run Code Online (Sandbox Code Playgroud)
该helloWorld.log();部分没有像预期的那样工作,但我希望这样做,但我不知道如何.
(function($) {
$.fn.log= function() {
console.log($(this));
};
})(jQuery);
Run Code Online (Sandbox Code Playgroud)
var log = function() {
console.log($(this));
}
Run Code Online (Sandbox Code Playgroud)
jQuery.fn.log = function(){
console.log($(this));
}
Run Code Online (Sandbox Code Playgroud)
TypeError:helloWorld.log不是函数
如果要在hello world变量(即String)上打印它,可以使用prototype函数为Strings提供日志方法.
例如:
String.prototype.log= function() {
console.log(this.toString());
});
Run Code Online (Sandbox Code Playgroud)
然后当你调用helloWorld.log()它应该调用这个函数.