简体   繁体   中英

Javascript logic syntax

My task was to create an object/class called MrFreeze and mark this object as frozen so that no other changes can be made to it.] I came up with the following solution which worked:

Object.freeze(MrFreeze);

But then as I viewed the solutions provided on http://www.codewars.com I came across this code:

(Object.freeze || object)(MrFreeze);

I fail to understand this line of code. Please explain why this works when using a OR object operator.

Seems like a fallback to me.

when Object.freeze does exist it performs:

Object.freeze(MrFreeze);

when Object.freeze doesn't exist it performs:

object(MrFreeze);

This might as well be written as (more verbose for clearification):

function freeze(MrFreeze, object) {
    if (Object.freeze) {
        Object.freeze(MrFreeze);
    }
    else {
        object(MrFreeze);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM