This is the 18th day of my participation in the August Challenge.

JS operator.

// What is the result of executing the following code
console.log(1 < 2 < 3);
console.log(3 > 2 > 1);
Copy the code

Answer: True false

instructions

  • 1<2 is true, which is implicitly converted to 1, 1<3 is true;
  • 3>2 is true, and here it is implicitly converted to 1, 1>1 is false

JS object cyclic reference

// What is the result of executing the following code
var obj1 = {
  a: 123
}
var obj2 = {
  b: 456
}
obj1.c = obj2;
obj2.c = obj1;
const jsonstr = JSON.stringify(obj1);
console.log(jsonstr)
Copy the code

The answer:

// an error will be reported when json.stringify
Uncaught TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Object'|property 'c' -> object with constructor 'Object'-- -- --property 'c' closes the circle
    at JSON.stringify (<anonymous>)
    at <anonymous> : 9:22Copy the code

instructions

Json. stringify and json. parse cannot serialize the loop reference JSON object

It is mentioned in MDN

  • An exception TypeError (” Cyclic Object Value “) is thrown when a cyclic reference is made
  • When attempting to convert a value of type BigInt, TypeError is raised (“BigInt value can’t be serialized in JSON”).

JS object property assignment operation

var obj = { a: 1 }
obj.b = obj = {
  a: 2
}
console.log(typeof obj.b);
Copy the code

Answer: undefined

instructions

    1. The “.” symbol takes precedence over the “=” symbol
    1. Execute obj.b to create an attribute “b” with the value undefined
    1. Obj points to {a: 2}
    1. Obj. B is assigned {a: 2}
    1. Because obj is not pointing to the original object, obj.b is undefind

The Generator of JS

function* generator(a) {
  let length = a.length;
  for (let i = 0; i < length; i++) {
    let item = a[i];
    if (typeofitem ! = ='number') {
      yield* generator(item);
    } else {
      yielditem; }}}const tasks = generator([[[4].5.6], [1].0]);
let result;
while(result = tasks.next(), result.done ! = =true) {
  console.log(result.value)
}
Copy the code

The answer:

4
5
6
1
0
Copy the code

instructions

// This is a Generator that turns a multidimensional array into a one-dimensional array
// Generator, which returns Generator objects
// The generator object can be accessed via.next for further operations
function* generator(a) {
  let length = a.length;
  // Iterate over the multidimensional array
  for (let i = 0; i < length; i++) {
    let item = a[i];
    // If the current array element is not a number, it is an array
    if (typeofitem ! = ='number') {
      This function generator iterates
      yield* generator(item);
    } else {
      // If it is a number, return the number directly
      console.log(item,'aaa')
      console.log(i,'i')
      yielditem; }}}Tasks.next () ¶
// Read the data returned by the iteration
const tasks = generator([[[4].5.6], [1].0]);
let result;
while(result = tasks.next(), result.done ! = =true) {
  // Print out the returned data according to the iterator
  console.log(result.value)
}
Copy the code