gpt4 book ai didi

Javascript 类 JQuery 库

转载 作者:行者123 更新时间:2023-11-30 10:38:55 24 4
gpt4 key购买 nike

我正在尝试模仿 JQuery 库(用于学习目的),但我遇到了以下问题:

function _(id){
var about={
version:"1.0",
author:"Samson"
}
if (this===window)
return new _(id);
if (!id)
return about;
if (typeof id=="string")
this.element=document.getElementById(id);
else
this.element=id;

return this;
}

_.prototype={
append:function(str){
this.element.innerHTML+=str;
return this;
},
post:function(url){
alert('posting to: '+url);
}
}

我可以这样使用:

_("a1").append(' deescription');

但我希望能够在不调用构造函数的情况下使用 post 函数:

_.post('url') //post is in the prototype but is undefined because the constructor is not called right? 

最佳答案

您需要在 _ 本身而不是其原型(prototype)上定义 post

_.post = function(url) { ... }

关于Javascript 类 JQuery 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12334607/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com