PHP 목록()과 동등한 Javascript
그 기능이 정말 마음에 들어요.
$matches = array('12', 'watt');
list($value, $unit) = $matches;
그것과 동등한 Javascript가 있나요?
Javascript의 'newer' 버전에는 다음이 있습니다.임무 파괴 - Javascript 1.7.Mozilla 기반 브라우저에서만 지원되며 Rhino에서도 지원될 수 있습니다.
var a = 1;
var b = 3;
[a, b] = [b, a];
편집: 사실 V8 Javascript 라이브러리(그리고 Chrome)가 이를 지원한다고 해도 놀라지 않을 것입니다. 그러나 현재 모든 최신 브라우저에서 지원됩니다(물론 IE 제외).
이것을 시험해 보세요.
matches = ['12', 'watt'];
[value, unit] = matches;
const matches = ['12', 'watt'];
const [value, unit] = matches;
Javascript에서 List/Explode를 사용할 수 있는 솔루션입니다.바이올린의 동작 예
첫 번째 구현:
var dateMonth = "04/15";
dateMonth.split("/").list("month","day", "year");
month == "04";
day == "15";
year == null;
또한 새로 생성된 변수의 범위도 지정할 수 있습니다.
var scoped = (function()
{
var dateMonth = "07/24/2013";
dateMonth.split("/").list("month","day", "year", this);
this.month == "07";
this.day == "24";
this.year == "2013";
})();
이는 어레이 프로토타입을 수정함으로써 실현되었습니다.
Array.prototype.list = function()
{
var
limit = this.length,
orphans = arguments.length - limit,
scope = orphans > 0 && typeof(arguments[arguments.length-1]) != "string" ? arguments[arguments.length-1] : window
;
while(limit--) scope[arguments[limit]] = this[limit];
if(scope != window) orphans--;
if(orphans > 0)
{
orphans += this.length;
while(orphans-- > this.length) scope[arguments[orphans]] = null;
}
}
의 실험적인 실장이 있다.list()여기 PHPJS에 의해:
https://github.com/kvz/phpjs/blob/master/_experimental/array/list.js
CoffeeScript는 다음과 같은 구문을 사용하여 파괴적인 할당을 제공합니다.
[a, b] = someFunctionReturningAnArray()
이는 매우 새로운 JavaScript 버전에서 제공되는 기능과 거의 동일합니다.그러나 CoffeeScript는 IE6의 JavaScript 엔진과도 호환되는 컴파일된 JS를 생성하므로 호환성이 중요한 경우 좋은 옵션입니다.
대부분의 JavaScript 구현은 아직 이 기능을 지원하지 않기 때문에 단순히 JavaScript와 같은 방식으로 실행할 수 있습니다.
function list(){
var args = arguments;
return function(array){
var obj = {};
for(i=0; i<args.length; i++){
obj[args[i]] = array[i];
}
return obj;
};
}
예:
var array = ['GET', '/users', 'UserController'];
var obj = {};
obj = list('method', 'route', 'controller')(array);
console.log(obj.method); // "GET"
console.log(obj.route); // "/users"
console.log(obj.controller); // "UserController"
또는 Array.protype에 list-method를 추가하는 방법도 있습니다(권장하지 않습니다).
Array.prototype.list = function(){
var i, obj = {};
for(i=0; i<arguments.length; i++){
obj[arguments[i]] = this[i];
}
// if you do this, you pass to the dark side `,:,´
this.props = obj;
return obj;
};
예:
/**
* Example 1: use Array.prototype.props
*/
var array = ['GET', '/users', 'UserController'];
array.list('method', 'route', 'controller');
console.log(array.props.method); // "GET"
console.log(array.props.route); // "/users"
console.log(array.props.controller); // "UserController"
/**
* Example 2: use the return value
*/
var array = ['GET', '/users', 'UserController'];
var props = array.list('method', 'route', 'controller');
console.log(props.method); // "GET"
console.log(props.route); // "/users"
console.log(props.controller); // "UserController"
이것은 내가 할 수 있는 것이다; 나는 그것을 하는 함수를 쓰지 않고 그것을 얻을 수 있는 한 짧게.하지만 "이것"의 범위를 조심해야 합니다.
list = ["a","b","c"];
vals = [1,2,3];
for(var i in vals)this[list[i]]=vals[i];
console.log(a,b,c);
웃기에 충분합니다.여전히 각 변수를 한 번에 하나씩 할당합니다.
a=vals[0];
b=vals[1];
c=vals[2];
이쪽이 훨씬 짧아요.또한 변수가 여러 개 있는 경우 모두 개별적으로 선언하지 말고 어레이에 저장하거나 폐쇄 속성을 지정하는 것이 좋습니다.
function list(fn,array){
if(fn.length && array.length){
for(var i=0;i<array.length;i++){
var applyArray = [];
for(var j=0;j<array[i].length;j++){
fn[j] = array[i][j];
applyArray.push(fn[j]);
}
fn.apply(this,applyArray);
}
}
}
예:
//array array mixture for composure
var arrayMixture = [ ["coffee","sugar","milk"], ["tea","sugar","honey"] ];
//call our function
list(function(treat,addin,addin2){
console.log("I like "+treat+" with " + addin + " and " + addin2);
},arrayMixture);
//output:
//I like coffee with sugar and milk
//I like tea with sugar and honey
언급URL : https://stackoverflow.com/questions/1954426/javascript-equivalent-of-phps-list
'source' 카테고리의 다른 글
| Python 진행 표시줄 (0) | 2023.01.15 |
|---|---|
| Python 함수에서 두 개의 값을 반환하려면 어떻게 해야 합니까? (0) | 2023.01.15 |
| InnoDB는 문자열을 어떻게 저장합니까? (0) | 2023.01.15 |
| Brew 목록 서비스를 실행할 때 Brew-error 256(mariadb의 경우)의 원인을 찾습니다. (0) | 2023.01.15 |
| MySQL 시스템 테이블을 최신 버전에서 InnoDB로 변환할 수 있습니까? (0) | 2023.01.15 |