In a JavaScript file I saw: function Somefunction(){ var that = this; ... } What is the purpose of declaring that and assigning it to this ? I'm going to begin this answer with an illustration: var colours = ['red', 'green', 'blue']; document.getElementById('element').addEventListener('click', function() { // this is a reference to the element clicked on var that = this;
在我看到的JavaScript文件中: function Somefunction(){ var that = this; ... } 什么是声明的目的that并将其分配给this ? 我将以一个例子开始这个答案: var colours = ['red', 'green', 'blue']; document.getElementById('element').addEventListener('click', function() { // this is a reference to the element clicked on var that = this; colours.forEach(function() { // this i
这里的'fn'是什么意思? window.jQuery.fn.jquery In jQuery, the fn property is just an alias to the prototype property. The jQuery identifier (or $ ) is just a constructor function, and all instances created with it, inherit from the constructor's prototype. A simple constructor function: function Test() { this.a = 'a'; } Test.prototype.b = 'b'; var test = new Test(); test.a; /
这里的'fn'是什么意思? window.jQuery.fn.jquery 在jQuery中, fn属性只是prototype属性的别名。 jQuery标识符(或$ )只是一个构造函数,并且使用它创建的所有实例都继承于构造函数的原型。 一个简单的构造函数: function Test() { this.a = 'a'; } Test.prototype.b = 'b'; var test = new Test(); test.a; // "a", own property test.b; // "b", inherited property 一个类似于jQuery体系结构的简单结构
Given this snippet of JavaScript... var a; var b = null; var c = undefined; var d = 4; var e = 'five'; var f = a || b || c || d || e; alert(f); // 4 Can someone please explain to me what this technique is called (my best guess is in the title of this question!)? And how/why it works exactly? My understanding is that variable f will be assigned the nearest value (from left to right) of the
鉴于这段JavaScript的片段...... var a; var b = null; var c = undefined; var d = 4; var e = 'five'; var f = a || b || c || d || e; alert(f); // 4 有人可以向我解释这种技术被称为什么(我最好的猜测是在这个问题的标题!)? 以及如何/为什么它的工作原理? 我的理解是,变量f将被赋予第一个变量的最接近的值(从左到右),该变量的值不是null或未定义,但我没有设法找到关于此技术的很多参考资料,已经看到它用
I'm trying to convert a regular function into function with namespacing. This is the regular function: surprise(); function surprise() { alert('TEST'); } and this is the same function with namespacing: var namespace = {}; namespace.surprise(); namespace.surprise = function () { alert('TEST'); }; I get the following error: Uncaught TypeError: namespace.surprise is not a fun
我想通过命名空间将常规函数转换为函数。 这是常规功能: surprise(); function surprise() { alert('TEST'); } 这与namespacing的功能相同: var namespace = {}; namespace.surprise(); namespace.surprise = function () { alert('TEST'); }; 我得到以下错误: Uncaught TypeError: namespace.surprise is not a function 如果你在函数定义之前调用,你会得到一个TypeError 。 您必须首先定义该功能。
I read an article regarding the various ways to namespace in JavaScript. I quite liked the look of the last one but JSLint thinks otherwise. I know JSLint can be overzealous but is there a way to make this technique play nice with it? var Something = {}; (function () { "use strict"; this.helloWorld = function () { var greeting = "Hello World!"; }; }.apply(Something)); 试
我阅读了一篇关于JavaScript中命名空间的各种方法的文章。 我非常喜欢最后一个的外观,但JSLint认为不然。 我知道JSLint可能是过分热心的,但是有没有办法让这种技术和它一起玩呢? var Something = {}; (function () { "use strict"; this.helloWorld = function () { var greeting = "Hello World!"; }; }.apply(Something)); 试试这个: var Something = {}; (function (something) { "use str
My code is going to turn into a mess if I don't start using some sort of namespacing technique. I'm relatively new to programming large javascript projects but have significant experience with systems programming in C++/java/python etc. Basically I'm trying to identify which is the preferred method for creating javascript namespaces, and what the pros/cons are for each method. Fo
如果我不开始使用某种命名空间技术,我的代码将变成一团糟。 我对编程大型JavaScript项目比较陌生,但在C ++ / java / python等系统编程方面拥有丰富的经验。 基本上我试图确定哪个是创建JavaScript命名空间的首选方法,以及每种方法的优缺点。 例如,我可以使用以下三种方法之一: var proj.lib.layout = { "centreElem": function (elem, W, H){ }, "getAbsolutePosition": function (elem){ } };
I've been trying to further my understanding of javascript namespacing and prototype inheritance but i am running into an issue. An example of what i'm dealing with code wise: var namespace = { ex1: function () { }, ex2: function () { this.exvar1 = 0, this.exvar2 = 0; } } namespace.ex1.prototype = { method1: function () { }, method2: function
我一直试图进一步了解JavaScript的命名空间和原型继承,但我遇到了一个问题。 我正在处理代码明智的一个例子: var namespace = { ex1: function () { }, ex2: function () { this.exvar1 = 0, this.exvar2 = 0; } } namespace.ex1.prototype = { method1: function () { }, method2: function () { } }; namespace.ex2.prototype = { method1: function () { a
right now i am at a point where i feel that i need to improve my javascript skills because i already see that what i want to realize will get quite complex. I've iterrated over the same fragment of code now 4 times and i am still not sure if it's the best way. The task: A user of a webpage can add different forms to a webpage which i call modules. Each form provides different user i
现在我正处在一个地方,我觉得我需要提高自己的JavaScript技能,因为我已经看到我想要实现的东西会变得相当复杂。 我现在已经对同一段代码进行了4次测试,但我仍然不确定这是否是最好的方法。 任务: 网页用户可以添加不同的表单到我称为模块的网页。 每种形式都提供不同的用户输入,需要以不同的方式处理。 用户喜欢的表单/模块可以添加到表单列表中。 我目前的解决方案 为了使代码更具可读性和分离功能,我使用命名
I am bit confused with namespaces of function in javascript. Can I have function with same names? Thanks There is no official concept of a namespace in Javascript like there is in C++. However, you can wrap functions in Javascript objects to emulate namespaces. For example, if you wanted to write a function in a "namespace" called MyNamespace , you might do the following: var My
我有点困惑于JavaScript中的函数名字空间。 我可以有同名的功能吗? 谢谢 JavaScript中没有官方的命名空间概念,就像C ++中的一样。 但是,您可以用Javascript对象封装函数来模拟名称空间。 例如,如果您想在称为MyNamespace的“名称空间”中编写函数,则可以执行以下操作: var MyNamespace = {}; MyNamespace.myFunction = function(arg1, arg2) { // do things here }; MyNamespace.myOtherFunction = function()
I'm using ng-if to show and hide an element. When the element shows up, I want to call a service, that scrolls within the new element to a certain child (by Id). The problem is, that if I try to call my service function right after setting the element to visible, the DOM doesn't seem to be ready yet. var myApp = angular.module('myApp',[]); myApp.factory("ScrollService", function () {
我正在使用ng-if来显示和隐藏一个元素。 当元素出现时,我想调用一个服务,该服务在新元素内滚动到某个孩子(由Id)。 问题是,如果在将元素设置为可见之后尝试调用我的服务函数,那么DOM似乎还没有准备好。 var myApp = angular.module('myApp',[]); myApp.factory("ScrollService", function () { return { scroll: function (id) { console.log(document.getElementById(id)); } }; }