声明式 和 命令式 的区别?

声明式 和 命令式 的区别?

  • 声明式: 告诉“机器”你想要的是什么(what),让机器想出如何去做(how)。

    • 关注结果,不关注具体怎么实现

      (devops10) root@django:/opt/devops# python manage.py makemigrations
      <QuerySet [<UserProfile: admin>, <UserProfile: wangsk>]>
      No changes detected
      (devops10) root@django:/opt/devops# python manage.py migrate
      <QuerySet [<UserProfile: admin>, <UserProfile: wangsk>]>
      Operations to perform:
        Apply all migrations: admin, auth, book, contenttypes, sessions, users
      Running migrations:
        No migrations to apply.
      (devops10) root@django:/opt/devops# 
      
      (devops10) root@django:/opt/devops# python manage.py  sqlmigrate users 0001
      <QuerySet [<UserProfile: admin>, <UserProfile: wangsk>]>
      BEGIN;
      --
      -- Create model UserProfile
      --
      CREATE TABLE `users_userprofile` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `password` varchar(128) NOT NULL, `last_login` datetime(6) NULL, `is_superuser` bool NOT NULL, `username` varchar(150) NOT NULL UNIQUE, `first_name` varchar(30) NOT NULL, `last_name` varchar(150) NOT NULL, `email` varchar(254) NOT NULL, `is_staff` bool NOT NULL, `is_active` bool NOT NULL, `date_joined` datetime(6) NOT NULL, `name` varchar(20) NOT NULL, `phone` varchar(20) NOT NULL);
      CREATE TABLE `users_userprofile_groups` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `userprofile_id` integer NOT NULL, `group_id` integer NOT NULL);
      CREATE TABLE `users_userprofile_user_permissions` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `userprofile_id` integer NOT NULL, `permission_id` integer NOT NULL);
      ALTER TABLE `users_userprofile_groups` ADD CONSTRAINT `users_userprofile_gr_userprofile_id_a4496a80_fk_users_use` FOREIGN KEY (`userprofile_id`) REFERENCES `users_userprofile` (`id`);
      ALTER TABLE `users_userprofile_groups` ADD CONSTRAINT `users_userprofile_groups_group_id_3de53dbf_fk_auth_group_id` FOREIGN KEY (`group_id`) REFERENCES `auth_group` (`id`);
      ALTER TABLE `users_userprofile_groups` ADD CONSTRAINT `users_userprofile_groups_userprofile_id_group_id_823cf2fc_uniq` UNIQUE (`userprofile_id`, `group_id`);
      ALTER TABLE `users_userprofile_user_permissions` ADD CONSTRAINT `users_userprofile_us_userprofile_id_34544737_fk_users_use` FOREIGN KEY (`userprofile_id`) REFERENCES `users_userprofile` (`id`);
      ALTER TABLE `users_userprofile_user_permissions` ADD CONSTRAINT `users_userprofile_us_permission_id_393136b6_fk_auth_perm` FOREIGN KEY (`permission_id`) REFERENCES `auth_permission` (`id`);
      ALTER TABLE `users_userprofile_user_permissions` ADD CONSTRAINT `users_userprofile_user_p_userprofile_id_permissio_d0215190_uniq` UNIQUE (`userprofile_id`, `permission_id`);
      COMMIT;
      
      
  • 命令式编程: 命令“机器”如何去做事情(how),这样不管你想要的是什么(what),它都会按照你的命令实现。

    • 关注的是过程,一步步的去实现

      var numbers = [1,2,3,4,5]
      
      var doubled = []
      
      for(var i = 0; i < numbers.length; i++) {
      
        var newNumber = numbers[i] * 2
        doubled.push(newNumber)
      
      }
      console.log(doubled) //=> [2,4,6,8,10]
      
posted @ 2020-07-18 23:31  wangsk  阅读(171)  评论(0)    收藏  举报