Javascript-форум (https://javascript.ru/forum/)
-   Оффтопик (https://javascript.ru/forum/offtopic/)
-   -   Обсуждений тред (https://javascript.ru/forum/offtopic/47364-obsuzhdenijj-tred.html)

cyber 17.08.2015 21:38

trikadin, отличить примитивный тип от другого ?
есть ли простой способ?

nerv_ 18.08.2015 00:18

Цитата:

Сообщение от cyber
отличить примитивный тип от другого ?

typeof или уточкой:
function toString(any) {
    return Object.prototype.toString.call(any);
}

function noop() {}


alert(toString(1));
alert(toString('text'));
alert(toString(true));
alert(toString(Symbol()));
alert(toString(undefined));
alert(toString(null));


alert(toString(/1/));
alert(toString([]));
alert(toString({}));
alert(toString(noop));
alert(toString(new Date()));
alert(toString(new Map()));
alert(toString(new Set()));
alert(toString(new WeakMap()));
alert(toString(new WeakSet()));

кря-кря :)

cyber 18.08.2015 01:26

nerv_, я знаю, просто интересно можно ли как то это сделать не проверяя на всем примитивы?

Erolast 18.08.2015 11:18

function isPrimitive(value) {
  return !(value instanceof Object);
}

?

cyber 18.08.2015 11:40

Erolast,

function isPrimitive(value) {
  return !(value instanceof Object);
}


alert( isPrimitive( new String( "h" ) ) );

Erolast 18.08.2015 12:09

Так это и не строка-примитив, это String object. Там и другие странности есть:
console.log(new String("h") == new String("h")); //false
console.log(typeof new String("h") == "string"); //false

cyber 18.08.2015 12:13

Erolast, но посути это строка

Erolast 18.08.2015 12:21

Строка, но не примитив. Объект.

Erolast 18.08.2015 12:30

Если все же надо включать строки/числа/булевы-объекты (что крайне странно: они не ведут себя как примитивы), то можно так:
function isPrimitiveLike(value) {
    return value === null || value === undefined || [Boolean, Number, Symbol, String].indexOf(value.constructor) != -1;
}

nerv_ 18.08.2015 12:39

Есть код
class Parent {}

class Child extends Parent {
  constructor() {
    this.x = 1;
    super();
  }
}

на что бабель ругается
Цитата:

repl: Line 7: 'this' is not allowed before super()
5 | class Child extends Parent {
6 | constructor() {
> 7 | this.x = 1;
| ^
8 | super();
9 | }
10 | }
sandbox

1. Какого Лена Головач?
2. Разве это прописано в стандарте? Под "это" подразумеваю:
- обязательность вызова супер в ребенке в принципе
- обязательность вызова супер до работы с this в ребенке


Часовой пояс GMT +3, время: 15:37.