小编dem*_*nes的帖子

在javascript中扩展Array对象的方法

我尝试使用一些用户友好的方法(如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)

javascript arrays class extend

38
推荐指数
2
解决办法
5万
查看次数

数据结构STL容器的等价物

我研究数据结构,我想问一下STL容器的等价物.

例如

  • vector =动态数组
  • queue = queue
  • stack = stack
  • priority_queue =堆
  • list =链表
  • set = tree
  • slist =单链表
  • bit_vector = vector bool
  • map = pair
  • deque =?
  • multiset =?
  • multimap =?
  • hash_set =?
  • hash_map =?
  • hash_multiset =?
  • hash_multimap =?
  • 哈希=?
  • bit_set =?

c++ stl equivalent data-structures

7
推荐指数
1
解决办法
3067
查看次数

使用jQuery将文本分解为随机位置

有一些jQuery示例,如何破坏文本,如下所示:

http://jsfiddle.net/doktormolle/dNXVx/

我该如何反过来?

我想将span元素中的字母分解为随机位置.

javascript jquery animation text

4
推荐指数
1
解决办法
5029
查看次数

javascript:将参数传递给对象构造函数

我正在编写一个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是日期字符串,则忽略它.为什么?

javascript arguments object

4
推荐指数
1
解决办法
6715
查看次数

为什么不能在构造函数中捕获异常?

我有这个测试代码来处理构造函数中的异常.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;

c++ constructor exception

3
推荐指数
1
解决办法
274
查看次数

混合开源软件并在 GPL3、MIT、BSD 许可证下发布

我正在编写一个 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 兼容吗?

licensing open-source

1
推荐指数
1
解决办法
1532
查看次数