gpt4 book ai didi

javascript - $ 在 jQuery 之外

转载 作者:行者123 更新时间:2023-11-29 20:03:12 25 4
gpt4 key购买 nike

此应用 https://developer.mozilla.org/en-US/demos/detail/meet-me-there/launch接受上传的照片,将二维码附加到照片上,然后允许您分享它们。除了 QR 功能之外,我已经在 J​​avaScript 下方附加了所有内容。该应用程序不使用 jQuery,但在开始时它为 $.

  window.onload = function(){
var $ = function(id){
console.log(id);
return document.getElementById(id);
},

当我在上面的位置运行带有 console.log 的应用程序时,它表明相当多的“id”正在通过 console.log(id) 传递。当文件加载时,它会记录“surface”、“cam”和“upload”,当您使用该应用程序时,它会记录“result”、“sharer”、“uploadedURL”和许多其他内容。问题是我看不到所有内容如何不断通过 console.log 的函数传递,以便在代码中的那个点记录不同的“id”。因此,我想知道“$”在这种情况下的意义是什么(没有 jQuery)。具体来说,通过创建“$”函数,它是否会在任何其他带有 $ 的事件运行时调用,例如 $('upload').onclick = function(){...

它的工作方式是否类似于在 jquery 中使用 $.prototype.function() 添加原型(prototype)在 jquery 中的工作方式。如果是这样,如果没有 jQuery,它从哪里获得此功能。

window.onload = function(){
var $ = function(id){
console.log(id);
return document.getElementById(id);
},
canvas = $('surface'),
ctx = canvas.getContext('2d'),
watcher, loc='No location provided', located;

$('cam').onchange = function(event){
console.log(event);
console.trace();
var files = event.target.files,
file;

if (files && files.length > 0) {
file = files[0];
try {
var URL = window.URL || window.webkitURL || window.mozURL;
var imgURL = URL.createObjectURL(file);

var img = new Image();
img.id = "tester";

//Load it onto the canvas
img.onload = function() {
console.trace();

canvas.width = this.width;
canvas.height = this.height;
$('info').innerHTML = ("Width: " + this.width + "px, Height: " + this.height + "px");
$('result').width = 400;
$('result').height = (400 / (this.width/this.height)) >> 0;
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
var codeSize = (canvas.height/4) >> 0;
var imgn = new Image();
imgn.onload = function(){
ctx.drawImage(imgn, (canvas.width-5- codeSize), (canvas.height-5-codeSize), codeSize, codeSize);
$('result').src = canvas.toDataURL();
}
imgn.src = (QR.encode(loc, codeSize, 5));
}

img.src = imgURL;

} catch (e) {
console.log("error: " + e);
}
}
},
// borrowed this functionality from cgack's demo
// https://developer.mozilla.org/en-US/demos/detail/snap-and-share
$('upload').onclick = function(){
$('infomsg').style.display = 'block';
var url = "http://api.imgur.com/2/upload.json",
params = new FormData(),
http = new XMLHttpRequest();

params.append('key','29a8b1db1d8fda0df87006def2307242');
params.append('image',canvas.toDataURL().split(',')[1]);

http.open("POST", url);
http.onload = function() {
var url = JSON.parse(http.responseText).upload.links.imgur_page;
$('uploadedUrl').href = url;
$('uploadedUrl').innerHTML = url;
$('shareFb').href = ("http://www.facebook.com/sharer.php?u="+url);
$('shareTwitter').href = ("http://twitter.com/intent/tweet?url="+url);
$('sharer').style.display = 'block';
$('infomsg').style.display = 'none';

};
http.send(params);
console.log(params);
};
watcher = navigator.geolocation.watchPosition(function(position){
console.trace();
console.log("navigator");
loc = "geo:" + position.coords.latitude + "," +position.coords.longitude;
located = true;
}, function(error){
if(error.code == error.PERMISSION_DENIED || error.code == error.POSITION_UNAVAILABLE)
alert('damn, we were not able to locate you. sorry.');
}
);
};

最佳答案

$ 只是一个变量名,和其他变量名一样。没有特殊意义。

"Problem is that I don't see how everything keeps getting passed through that function for the console.log to log the 'id' at that point in the code."

任何时候你看到$,它都是对函数的引用。所以 () 在它用给定的参数调用它之后。它就像任何其他函数一样,只是引用了一个有趣的名称。

"Therefore, I wonder what the significance of '$' is in this context (no jQuery). Specifically, by creating that '$' function, is it called anytime that any of the other events with $ are run"

同样,没有实际意义。它只是一个函数名。如果您将 $ 的所有实例重命名为 byId,它的行为将相同。

window.onload = function(){
// var $ = function(id){
var byId = function(id){
console.log(id);
return document.getElementById(id);
},
canvas = foo('surface'),
ctx = canvas.getContext('2d'),
watcher, loc='No location provided', located;

byId('cam').onchange = function(event){
/* a bunch of code */
},
byId('upload').onclick = function(){
/* a bunch of code */
};
// rest of the code
};

关于javascript - $ 在 jQuery 之外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13278386/

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