JS:符号优先级

在JavaScript中符号的优先级一般从高到低排序为:

 

1、关于new带参和无参的符号优先级顺序:

          function  fn () {
                return function(){
                        console.log(666);
                        return [1,3,20] ;
                        }
            }
            
            var f1=new fn;
            console.log(f1);
            
            
            var f2=fn();
            console.log(f2);
            
            var f3=new fn();

            console.log(f3);
            
            var f4=new f3;
            console.log(f4);
            
            var f5=new f3();
            console.log(f5);
            

打印结果:

ƒ (){console.log(666);return [1,3,20] }
ƒ (){console.log(666);return [1,3,20] }
ƒ (){console.log(666);return [1,3,20] }
666   [1, 3, 20] 

666   [1, 3, 20]

 

理解:

f1是创建了一个新对象;

f2是普通调用;

f3是new带参,创建一个新对象,并调用,返回了一个函数;

f4是new无参,调用fn返回的那个函数;

f5是new带参,创建了一个新对象,调用fn返回的函数,并得到那个函数的返回值,如果是引用数据则返回,如果是基本数据,会返回函数的this对象

 

注:

当我们遇到:

new obj.function()-- > (new (obj.function) ( ) )

new new obj.function() () -- > new (  new  (obj.function)  ( )  ) ( ) 

 

posted on 2022-06-22 15:44  香香鲲  阅读(344)  评论(0)    收藏  举报