我创建了一个javascript类如下:
var MyClass = (function() {
function myprivate(param) {
console.log(param);
}
return {
MyPublic : function(param) {
myprivate(param);
}
};
})();
MyClass.MyPublic("hello");
Run Code Online (Sandbox Code Playgroud)
上面的代码是有效的,但我的问题是,如果我想将命名空间引入该类.
基本上我希望能够像这样调用类:
Namespace.MyClass.MyPublic("Hello World");
Run Code Online (Sandbox Code Playgroud)
如果我添加了Namespace.MyClass,它将抛出错误"语法错误".我确实尝试添加"window.Namespace = {}",但它也不起作用.
谢谢.. :)
我有以下情况.
我开发了我的第一个MVC Asp.Net应用程序.它在我的服务器上运行以下地址
http://localhost:59441/
Run Code Online (Sandbox Code Playgroud)
我写了一些看起来像这样的JQuery Post Methods
$.ajax({
type: "POST",
url: "/CeduleGlobale/UpdateCheckBox", ...
Run Code Online (Sandbox Code Playgroud)
CeduleGlobale是我的ControllerName,UpdateCheckBox是我的methodName
当我将应用程序放在testServer上时,它被放在VirtualDirectory中
因此现在申请
http://testServer/JprApplication/
Run Code Online (Sandbox Code Playgroud)
没有更多要指定的端口以及应用程序名称
当我开始测试时,我很快发现我的JQuery Post调用不再起作用了......
我修改了它们,所以现在URL是
/JprMvc/CeduleGlobale/UpdateCheckBox
Run Code Online (Sandbox Code Playgroud)
问题是2倍.
我确信我遗漏了一些基本的东西来简化这个.
谢谢
我一直看到在AngularJS中创建控制器和服务的不同例子,我很困惑,有人可以向我解释这两种方法之间的差异吗?
app.service('reverseService', function() {
this.reverse = function(name) {
return name.split("").reverse().join("");
};
});
app.factory('reverseService', function() {
return {
reverse : function(name) {
return name.split("").reverse().join("");
}
}
});
Run Code Online (Sandbox Code Playgroud)
还有一个控制器示例:
function ExampleCtrl($scope) {
$scope.data = "some data";
}
app.controller("ExampleCtrl", function($scope) {
$scope.data = "some data";
}
Run Code Online (Sandbox Code Playgroud) 我一直对Java和C#如何处理命名空间的概念感到困惑.
首先,一些编程语言中命名空间污染的例子:
using namespace std 对于C++.
业内人士对此表示不满,但在开始编程时,初学者仍然被教导要这样做.这是一个关于处理全局命名空间的建议的问题
import math.* 在Python中
在使用Python类时,我被告知不建议这样做,因为它污染了命名空间,并且首先允许访问数学库中的所有方法,Math.functionname但在编写具有重复名称的方法时可能会导致冲突.它显然导致了解释器的更多工作,因为它导入了所有函数,甚至那些未使用的函数.
问题:
在C#和Java中是否存在"命名空间污染"(即导入大量可能在编写方法时引发冲突的方法)(它们在很多方面类似)?为什么不?
当然,相关问题过于相似而无法提出另一个问题:
- 这是因为我们可能需要明确的@Override事情或某种预防措施?
- 它存在,但它不是一个东西,因为它不会像'使用命名空间std'那样造成太大的破坏,而且我不知道它对于学术课程中的软件开发是否相对较新?
例
我发现自己using在C#中有很多库,以避免为变量重命名命名空间,比如XElement
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//the ones above are auto-generated by Visual Studio, too
using System.Xml.Linq;
Run Code Online (Sandbox Code Playgroud)
我会避免System.Xml.Linq.XElement每次创建XElement时都要做.就像我们std::cout在C++中总是要做的一样
或者在我常见的Java中:import java.util.linkedlist甚至import java.util.*
如果我的课程在别处使用,这些会不会导致命名空间污染?或者是因为它们只会"污染"特定的类范围而不是其他可能导入或继承我的类的类?
我试着寻找答案,但找不到一个,但我可能会错误地搜索搜索.
编辑4/20/2015:
正如@RealSkeptic所提到的,事实证明也不鼓励Java通配符导入.资源
除了下面接受的答案和答案之外,Java和C#中的导入本身也包含在内,因此即使有人使用通配符类型的导入将未使用的方法名称添加到命名空间,它也不会影响其他类.在类级别上,如果名称中发生冲突,Java和C#编译器将引发错误,引用模糊的导入,并且在问题解决之前无法进一步编译(例如通过重命名函数).
我现在有点困惑,因为我计划在我的一个项目中首次包含多个源文件和头文件.
所以我想知道这是否是正确的方法?
我是否必须在每个直接使用它的源文件中包含字符串标头?
那么Visual C++要我包含的"stdafx.hpp"标题呢?
这会是要走的路吗?
main.cpp中
#include "stdafx.hpp"
#include <string> //?
#include <stringLib1.h>
#include <stringLib2.h>
using std::string;
//use a windows.h function here
//use a stringLib1 function here
//use a stringLib2 function here
Run Code Online (Sandbox Code Playgroud)
stringLib1.h
#include "stdafx.hpp"
#include <string>
using std::string;
class uselessClass1
{
public:
string GetStringBack1(string myString);
};
Run Code Online (Sandbox Code Playgroud)
stringLib1.cpp
#include "stdafx.hpp"
string uselessClass1::GetStringBack1(string myString) {
return myString;
}
Run Code Online (Sandbox Code Playgroud)
stringLib2.h
#include "stdafx.hpp"
#include <string>
using std::string;
class uselessClass2
{
public:
string GetStringBack2(string myString);
};
Run Code Online (Sandbox Code Playgroud)
stringLib2.cpp
#include "stdafx.hpp"
string uselessClass2::GetStringBack2(string myString) { …Run Code Online (Sandbox Code Playgroud) c++ include standard-library header-files precompiled-headers
我理解嵌套函数是什么,但我不明白为什么我们甚至首先需要嵌套函数.是否存在只能通过在JavaScript中使用嵌套函数来解决的问题.我看到的所有创建嵌套函数的示例都可以在不在函数内部创建函数的情况下进行编码,并且结果相同.那么哪个问题需要创建嵌套函数,并且只能通过使用嵌套函数来有效地解决.
"命名空间污染"一词的含义是什么?为什么静态方法有助于防止它?
这个问题看起来很相似,但具体与JavaScript有关,答案并没有定义这个术语.
我对 JavaScript 中的作用域和闭包的概念有很好的理解。
此外,以下站点提供了如何实现 JavaScript 命名空间的示例:
我仍然不明白的是,似乎有多少人混淆了作用域和命名空间的概念。此外,同样的人还经常提到如何不“污染全局命名空间”而不是“在全局范围内创建全局变量/变量”。
问题
let/ 的块作用域const)bar避免污染全局命名空间(除了foo),但它bar仍然在全局范围内:var foo = { bar: 42 }我必须"获取"一个数组,但我拥有的是一个与数组名称匹配的字符串.显然这种事情不起作用,但它显示了我正在尝试做的事情:
var arrayname = new Array(1, 2, 3);
var array = 'arrayname';
Alert(array[0]);
Run Code Online (Sandbox Code Playgroud)
当然,上面的例子是'a'而不是1,就像我需要的那样.
背景是我正在使用Hyperion Business Intelligence仪表板,其中使用的数组由用于调用它的按钮名称的子字符串确定.
我正在尝试同时或并行发出两个或多个单独的 AJAX 请求。
当所有请求完成并返回所有 AJAX 请求的数据时,我想立即调用一个包含所有数据的函数。
*尝试使用以下 AJAX 风格:
function Ajax(handleData) {
$.ajax({
url: url,
type: 'POST',
data: data_ajax,
success:function(data) {
handleData(data);
},
error: function(e){
console.log(e.message);
}
});
}
Ajax(function(output){
response_parsed = JSON.parse(output);
do stuff like assign to global var
}
Run Code Online (Sandbox Code Playgroud)
* 尝试使用以下 AJAX 风格:
get_array = (function(){
var response_parsed;
$.ajax({
url: url,
type: 'POST',
data: data_ajax,
success:function(data) {
response_parsed = JSON.parse(data);
},
error: function(e){
console.log(e.message);
},
});
return {getData …Run Code Online (Sandbox Code Playgroud) 我发现我有两个函数共享一些代码所以我决定把它放在模板函数中:
function template(callback){
var all, these, variables, are, used, inthe, callbackFunction;
for (var i=0; i<10; i++){
callback();
}
}
function myFirstFunction(){
//do something with all those variables
}
function mySecondFunction(){
//do something else
}
Run Code Online (Sandbox Code Playgroud)
因此,对于每一个我称之为功能template(myFirstFunction)和template(mySecondFunction)
有没有什么方法可以使用我的函数中的模板函数中定义的所有变量而不通过参数传递它们?
我的函数实际上是一个对象的方法:
function MyObject(){
};
MyObject.prototype.template = function(){
var all, these, variables, are, used, inthe, callbackFunction;
for (var i=0; i<10; i++){
callback();
}};
MyObject.prototype.myFirstMethod = function(){
this.template(function(){
//doSomething with all those variables
});
};
MyObject.prototype.mySecondMethod = function(){ …Run Code Online (Sandbox Code Playgroud) javascript ×7
namespaces ×3
c++ ×2
ajax ×1
angularjs ×1
asp.net-mvc ×1
asynchronous ×1
c# ×1
function ×1
header-files ×1
hyperion ×1
include ×1
java ×1
jquery ×1
scope ×1
url-routing ×1
variables ×1