相关疑难解决方法(0)

Javascript中的函数重载 - 最佳实践

在Javascript中伪造函数重载的最佳方法是什么?

我知道不可能像在其他语言中一样重载Javascript中的函数.如果我需要一个函数有两个用途foo(x)foo(x,y,z)这是最好的/优选的方法:

  1. 首先使用不同的名称
  2. 使用可选参数 y = y || 'default'
  3. 使用参数数量
  4. 检查参数的类型
  5. 或者怎么样?

javascript overloading

739
推荐指数
13
解决办法
40万
查看次数

如何在ActionScript中重载函数?

我想要一个能够采用各种类型的功能.AS3不支持直接重载...所以我不能执行以下操作:

//THIS ISN'T SUPPORTED BY AS3

function someFunction(xx:int, yy:int, someBoolean:Boolean = true){
    //blah blah blah
}
function someFunction(arr:Array, someBoolean:Boolean = true){
    someFunction(arr[0], arr[1], someBoolean);
}
Run Code Online (Sandbox Code Playgroud)

我如何解决它仍然有一个能够采取各种类型的参数的功能?

actionscript overloading actionscript-3

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

允许在Javascript中使用命名参数或位置参数

我怎样才能有一个函数接受或者命名参数(foo({a: 'hello', b: 'it is me'}))位置参数(foo('hello', 'it is me'))?

我知道可以通过将对象传递给函数来模拟命名参数:

function foo(options) {
    options = options || {};
    var a = options.a || 'peanut'; // whatever default value
    var b = options.b || 'butter'; // whatever default value
    console.log(a, b);
}

// ES6 allows automatic destructuring
function foo({a = 'peanut', b = 'butter'} = {}) {
    console.log(a, b);
}
Run Code Online (Sandbox Code Playgroud)

但这不允许我接受通过的位置论证.

我想使用ES6,但ES5的任何东西都可以.

javascript parameter-passing ecmascript-6

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

带有可选参数的JavaScript函数

我是来自Python背景的JavaScript的新手.在Python中,参数可以作为键和值传递:

def printinfo( name, age = 35 ):
   print "Name: ", name
   print "Age ", age
   return;
Run Code Online (Sandbox Code Playgroud)

然后可以这样调用该函数:

printinfo( age=50, name="miki" )
printinfo( name="miki" )
Run Code Online (Sandbox Code Playgroud)

这些参数可以在JavaScript函数中传递吗?

我希望能够传递一个或多个参数.例如一个JavaScript函数:

function plotChart(data, xlabel, ylabel, chart_type="l"){
    ...
} 
Run Code Online (Sandbox Code Playgroud)

我希望能够只传递数据和图表类型,标签是可选的,例如:

plotChart(data, chart_type="pie")
Run Code Online (Sandbox Code Playgroud)

这可以用JavaScript吗?

javascript named-parameters default-parameters

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

Javascript-以可选参数作为对象的函数?

我需要一个函数,它的参数是一个对象,如果我将其保留为空,它将加载默认值。

就像是:

function loadMap(args) { //args is an object and its optional

   //this is what I want to do, test args and load defauts if necesary
   /*
   pseudocode:
   if args.latitude is not set then
       give it a default value

   if args.longitude is not set then
       give it a default value

    end pseudocode */

   alert("Latitude is "+args.latitude );
   alert("Longitude is "+args.longitude );
}

//outputs the default values
loadMap();

//outputs custom values
loadMap({latitude: "x.xxxx", longitude: "-x.xxxx"});

//and should work too and output …
Run Code Online (Sandbox Code Playgroud)

javascript parameters object optional

3
推荐指数
2
解决办法
5094
查看次数

在 ES6 中创建多个构造函数

在 ES5 中,可以为一个类创建多个构造函数,同时使用原型为两者保留公共部分,如下所示

function Book() {
    //just creates an empty book.
}


function Book(title, length, author) {
    this.title = title;
    this.Length = length;
    this.author = author;
}

Book.prototype = {
    ISBN: "",
    Length: -1,
    genre: "",
    covering: "",
    author: "",
    currentPage: 0,
    title: "",

    flipTo: function FlipToAPage(pNum) {
        this.currentPage = pNum;
    },

    turnPageForward: function turnForward() {
        this.flipTo(this.currentPage++);
    },

    turnPageBackward: function turnBackward() {
        this.flipTo(this.currentPage--);
    }
};

var books = new Array(new Book(), new Book("First Edition", 350, "Random"));
Run Code Online (Sandbox Code Playgroud)

我想使用 ES6 类和构造函数语法实现相同的结果

class …
Run Code Online (Sandbox Code Playgroud)

javascript ecmascript-6 es6-class

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