1. What is the relationship between objects and functions?

From today onwards, I will take you to pretend to be coercive and take a look at the advanced and advanced js. If you are weak in js, I suggest that you should make up for the basic knowledge of js first.

First, recognize the typeof operator and instanceof

1. typeof: This is your very friendly friend

When we judge the type of a variable in writing conditional judgment, we are talking about the basic type here, and we often use the typeof unary operator, for example:

  1. if(typeof num ==='undefined'){
  2. //do something .........
  3. }

I will list the values ​​returned by typeof for you:

  1. console.log(typeof a);//undefined
  2. console.log(typeof'a');//string
  3. console.log(typeof10);//number
  4. console.log(typeoftrue);//boolean
  5. //以下返回的是一个对象类型
  6. console.log(typeoffunction(){});//function
  7. console.log(typeof[1,2,'3']);//object
  8. console.log(typeof{name:'zhangjl'});//object
  9. console.log(typeofnull);//object

 

But when you use it, you need to pay attention to the following issues:

(1) The types returned by typeof are all strings. Remember to be strings. Many students may not add quotation marks .

(2), only the value type can be judged .

(3) The parameters that need to be judged by typeof can be parenthesized or not (typeof (a) or typeof a). In actual development, you can choose not to add parentheses.

Since typeof is so powerful, how does it judge that it is a reference type? This is relatively weak, because when checking the reference type, whether it is an array (for arrays, he defines a new function similar to syntactic sugar in ES5, that is isArray(), the returned data type is Boolean type) or Functions, or non-value types, all return object. Then we need the following instanceof to judge a reference type.

2、instanceof

It comes in handy when you determine whether a variable is a reference to an instance of an object. E.g:

  1. console.log(user instanceof User)

Some people will ask, the function typeof returns a function, how can it become an object? This will be explained systematically below.

In order to solve the doubt, first look at an example:

  1. var a  =function(){
  2. //do something .......
  3. }
  4. a instanceof Object  //true
  5. functionPerson(){
  6. //do something .......
  7. }
  8. Person instanceof Object//true

From this we can see that functions do belong to objects.

Conclusion: The value type is judged by typeof, and the reference type is judged by instanceof.

2. Objects and functions

1. An object is a collection of properties    

关于对象,我们开发人员应该是深有体会,在java中不是有一句“一切事物皆对象”吗? 那么在JavaScript中是不是也是这样呢?我们来梳理一下关于对象的相关认识和理解。

    在java中new出来的才是对象,并且对象是一个有属性、有方法的完整体,但是在JavaScript中并不像java一样,除了new出来的是对象外,还有字面量的方式声明对象。更好玩的是你可以随时随地的在对象的构造函数外扩充属性和方法,字面量的在字面量对象的花括号外面扩充。这里我对创建对象的方式就不做说明了,我们主要看看对字面量创建JavaScript对象的扩充属性,其他的创建对象的方式中也是可以通过扩充属性的方式扩充对象的属性。

  1. var obj ={
  2. sum:10,
  3. user:{
  4. name:'zhangjl',
  5. age:18,
  6. dep:'软件事业部'
  7. },
  8. add:function(){
  9. //do something.........
  10. },
  11. arry:['学历','姓名','专业']
  12. }
  13. obj.update =function(){
  14. //do something .....
  15. }

    以上这种方式使用字面量方式创建对象,这里的obj对象拥有四个属性:sum、user、add、arry,甚至user对象内部又是一个对象。所以对象其实就是一个属性的集合。

2、对象和函数的关系  

函数和对象之间的那些说不清楚的关系有点让人难以接受。为什么呢?咱们再看两个例子:

  1. functionPerson(name,age){
  2. this.name = name;
  3. this.age = age;
  4. }
  5. var zhangjl =newPerson('zhangjl',18);
  6. var wangwu =newPerson('wangwu',20);
  1. function person(){
  2. console.info('这个是一个普通的函数');
  3. }
  4. person();

创建对象的是构造函数,创建普通函数的是函数声明。说到函数声明我又得不得不说函数表达式,两个是有区别的。

关于函数声明和函数表达式的区别,直接上例子:

 

  1. var a =function(){
  2. console.info(name);
  3. }()
  4. function b(){
  5. console.info(name)
  6. }
  7. var name='zhangjl';
  8. a;//undefined
  9. b();//zhangjl

结果令人很惊,因为在 JavaScript在拿到这个脚本的时候,js在解释器(js引擎)在解析脚本之前对函数声明和变量需要调整先后位置的,函数表达式可不会调整先后位置。

结论:函数是构造对象的其中一种方式,函数也可以是很普的函数。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326158859&siteId=291194637