简体   繁体   中英

Is it possible to check if a variable is equal to window object or not?

What I'm trying to do an example:

let var1 = window;

if(var1 == window){
    //do something
}

Will it operate good?

Not asking this:

let a = {c: 'a'};
let b = {c: 'a'};

if(a == b){
// do something
}

There is a list in responsive page. When the page is loaded, if page width lower than 768px function gets window object for detect scroll position like this:

if((window.innerWidth || document.documentElement.clientWidth || document.documentElement.clientWidth) < 768){
  flowObj.scrollElement = window;
}else{
  flowObj.scrollElement = document.querySelector('.flow-list').parentNode;
}
// If width lower than 768px list-container CSS change to height: auto; scroll: none; so list scroll on body

Need to change scroll position with this:

if(flowObj.scrollElement == window){
 flowObj.scrollElement.scrollTo(0, value);
}else {
 flowObj.scrollElement.scrollTop = value;
}

Need to know is it working good?

let var1 = window;

Var1 is a reference pointing at the window object. That is, you only have a "key" to the window object itself. As long as you don't change var1's value, a shallow comparison with window will always return true

On the other hand, if you try to make a deep copy of the window object, it will get tricky because the window object changes over time, but the object you created by deep cloning won't.

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