我尝试使用一些用户友好的方法(如Array.Add()而不是Array.push()等在javascript中扩展Array对象...
我实现了3种方法.不幸的是,第三种方式不起作用,我想问为什么?以及如何做到这一点.
//------------- 1st way
Array.prototype.Add=function(element){
this.push(element);
};
var list1 = new Array();
list1.Add("Hello world");
alert(list1[0]);
//------------- 2nd way
function Array2 () {
//some other properties and methods
};
Array2.prototype = new Array;
Array2.prototype.Add = function(element){
this.push(element);
};
var list2 = new Array2;
list2.Add(123);
alert(list2[0]);
//------------- 3rd way
function Array3 () {
this.prototype = new Array;
this.Add = function(element){
this.push(element);
};
};
var list3 = new Array3;
list3.Add(456); //push is not a function
alert(list3[0]); // undefined
Run Code Online (Sandbox Code Playgroud)
在第三种方式,我想在内部Array3类扩展Array对象.如何做到这一点,以免"推不是一个功能"和"未定义"?
在这里,我添加第四种方式.
//------------- 4th …Run Code Online (Sandbox Code Playgroud) 我研究数据结构,我想问一下STL容器的等价物.
例如
我正在编写一个jquery lib,我需要实现Datetime函数.我需要创建一个返回新Date对象的.Date()函数.
如何将我的.Date(args)函数的参数传递给Date对象构造函数,以便创建一个新的日期对象?
我试过这样的东西,我是插件命名空间,我= $ .jqGUI.
//.Date(Value)
/* Return a date object.
* ReturnValue = Date(Value)
*/
me.Date = function(Value){
//pass arguments to Date constructor
var sDate = Date.prototype.constructor.apply(null, arguments);
console.log(sDate);
//create a date object d
var d = new Date(sDate);
//validate date object
if(d.toDateString()=="Invalid Date"){
throw new Error("$.jqGUI.Date: Invalid Date");
}
else{
return d;
}
};
Run Code Online (Sandbox Code Playgroud)
在这里我传递给Date.prototype.constructor参数,但我得到任何情况下的当前日期.如果arguments是日期字符串,则忽略它.为什么?
我有这个测试代码来处理构造函数中的异常.function f()创建一个异常除以零,但不捕获此异常.相反,如果我抛出一个自定义整数,则捕获异常.
#include <iostream>
using namespace std;
class A
{
public:
void f(){
int x;
x=1/0;
//throw 10;
}
A(){
try{
f();
}
catch(int e){
cout << "Exception caught\n";
}
}
};
int main (int argc, const char * argv[])
{
A a;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么我能抓住自定义投掷10; 而不是x = 1/0;
我正在编写一个 javascript 库,我想在 GPL3、MIT、BSD 三重许可下提供它。
我还发现了一个 Apache 2.0 开源项目,我想将其包含在我的项目中。
根据http://www.apache.org/licenses/GPL-compatibility.html Apache 2软件可以包含在GPLv3项目中,所以我的项目的GPL3版本是兼容的。
我可以在 MIT 和 BSD 项目中包含 Apache 2.0 代码,以便最终项目可以与三重许可证 GPL3、MIT、BSD 兼容吗?
javascript ×3
c++ ×2
animation ×1
arguments ×1
arrays ×1
class ×1
constructor ×1
equivalent ×1
exception ×1
extend ×1
jquery ×1
licensing ×1
object ×1
open-source ×1
stl ×1
text ×1