javascript - $ jQuery 之外

标签 javascript jquery

这个应用程序https://developer.mozilla.org/en-US/demos/detail/meet-me-there/launch接受上传的照片,附加二维码,然后允许您共享它们。我在下面附上了除 QR 功能之外的所有内容的 JavaScript。该应用程序不使用 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() 在 jquery 中添加原型(prototype)的方式。如果是这样,如果没有 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/

相关文章:

javascript - 仅在结束数据完整日历上的事件栏

JavaScript:通过 AJAX 下载和加载图表的脚本不起作用

javascript - D3.js:条形图中的条形,比例尺未正确建模数据

javascript - 使用javascript从外部css文件中获取元素的样式

JavaScript 隐藏和显示切换,隐藏 DIV

javascript - HAML JS 脚本中的内联变量

jQuery click 函数与内联 onclick

javascript - 为什么在 IE 中进行 setInterval div 替换后,我的容器会丢失其内容?

jquery - 我如何在 Angular 2 中使用 mdbootstrap?

javascript - Angular 动画: can't find query