框架和库的区别_框架和库之间的区别

框架和库的区别

Developers often use the terms “library” and “framework” interchangeably. But there is a difference.

开发人员经常互换使用术语“库”和“框架”。 但是有区别。

Both frameworks and libraries are code written by someone else that is used to help solve common problems.

框架和库都是由其他人编写的用于帮助解决常见问题的代码。

For example, let’s say you have a program where you plan on working with strings. You decide to keep your code DRY (don’t repeat yourself) and write some reusable functions like these:

例如,假设您有一个计划使用字符串的程序。 您决定保持代码干燥(不要重复自己),并编写一些可重复使用的函数,如下所示:

function getWords(str) {const words = str.split(' ');return words;
}
function createSentence(words) {const sentence = words.join(' ');return sentence;
}

Congratulations. You’ve created a library.

恭喜你 您已经创建了一个库。

There isn’t anything magic about frameworks or library. Both libraries and frameworks are reusable code written by someone else. Their purpose is to help you solve common problems in easier ways.

框架或库没有任何魔术。 库和框架都是别人编写的可重用代码。 他们的目的是帮助您以更简单的方式解决常见问题。

I often use a house as a metaphor for web development concepts.

我经常用一所房子作为Web开发概念的隐喻。

A library is like going to Ikea. You already have a home, but you need a bit of help with furniture. You don’t feel like making your own table from scratch. Ikea allows you to pick and choose different things to go in your home. You are in control.

图书馆就像去宜家。 您已经有了家,但是在家具方面需要一些帮助。 您不想从头开始制作自己的桌子。 宜家(IKEA)可让您挑选不同的物品放入家中。 一切尽在您的掌握之中。

A framework, on the other hand, is like building a model home. You have a set of blueprints and a few limited choices when it comes to architecture and design. Ultimately, the contractor and blueprint are in control. And they will let you know when and where you can provide your input.

另一方面,框架就像建立样板房。 当涉及到架构和设计时,您有一套蓝图和一些有限的选择。 最终,承包商和蓝图将得到控制。 他们会告诉您何时何地可以提供您的输入。

技术差异 (The Technical Difference)

The technical difference between a framework and library lies in a term called inversion of control.

框架和库之间的技术区别在于称为控制反转的术语。

When you use a library, you are in charge of the flow of the application. You are choosing when and where to call the library. When you use a framework, the framework is in charge of the flow. It provides some places for you to plug in your code, but it calls the code you plugged in as needed.

使用库时,您将负责应用程序的流程。 您正在选择何时何地调用库。 使用框架时,框架负责流程。 它为您提供了一些插入代码的位置,但是它会根据需要调用您插入的代码。

Let’s look at an example using jQuery (a library) and Vue.js (a framework).

让我们来看一个使用jQuery(一个库)和Vue.js(一个框架)的示例。

Imagine we want to display an error message when an error is present. In our example, we will click a button, and pretend an error occurs.

假设我们要在出现错误时显示一条错误消息。 在我们的示例中,我们将单击一个按钮,并假装发生错误。

使用jQuery: (With jQuery:)

// index.html
<html><head><script src="https://code.jquery.com/jquery-3.3.1.min.js"</script><script src="./app.js"></script></head><body><div id="app"><button id="myButton">Submit</button></div></body>
</html>
// app.js
// A bunch of our own code, 
// followed by calling the jQuery library
let error = false;
const errorMessage = 'An Error Occurred';
$('#myButton').on('click', () => {error = true; // pretend some error occurs and set error = trueif (error) {$('#app').append(`<p id="error">${errorMessage}</p>`);} else {$('#error').remove();}
});

Notice how we use jQuery. We tell our program where we want to call it. This is much like going to a physical library and pulling certain books off the shelf as we want them.

注意我们如何使用jQuery。 我们告诉程序我们要在哪里调用它。 这就像去实体图书馆并根据需要从书架上取出某些书。

That’s not to say jQuery functions don’t require certain inputs once we call them, but jQuery itself is a library of those functions. We are in charge.

这并不是说jQuery函数一旦调用它们就不需要某些输入,但是jQuery本身就是这些函数的库。 我们负责。

使用Vue.js (With Vue.js)

//index.html
<html><head><script src="https://cdn.jsdelivr.net/npm/vue"></script><script src="./app.js"></script></head><body><div id="app"></div></body>
</html>
const vm = new Vue({template: `<div id="vue-example"><button @click="checkForErrors">Submit</button><p v-if="error">{{ errorMessage }}</p></div>`,el: '#vue-example',data: {error: null,errorMessage: 'An Error Occurred',},methods: {checkForErrors()  {this.error = !this.error;},},
});

With Vue, we have to fill in the blanks. The Vue constructor is an object with certain properties. It tells us what it needs, and then behind the scenes, Vue decides when it needs it. Vue inverts the control of the program. We plug our code into Vue. Vue is in charge.

使用Vue,我们必须填补空白。 Vue构造函数是具有某些属性的对象。 它告诉我们它需要什么,然后在后台,Vue决定何时需要它。 Vue反转程序的控制权。 我们将代码插入Vue。 Vue负责。

The difference whether it is a library or framework is whether or not there is an inversion of control.

是库还是框架的区别在于控件是否反转。

关于“被调教”的说明 (A note on being “opinionated”)

You’ll often hear frameworks and libraries described as “opinionated” or “un-opinionated.” These terms are subjective. They attempt to define the level of freedom a developer has when structuring their code.

您经常会听到被描述为“已优化”或“未优化”的框架和库。 这些术语是主观的。 他们试图定义开发人员在构建代码时所具有的自由度。

Frameworks are more opinionated than not since — by definition — the inversion of control requires a concession of application-design freedom.

框架之所以固执己见,是因为(从定义上来说)控制权的倒置需要让应用程序设计自由。

Again, the degree to which something is opinionated is subjective. For example, I personally would consider Angular a highly opinionated framework, and Vue.js a less-opinionated framework.

同样,对某件事的看法是主观的。 例如,我个人将Angular视为一个自以为是的框架,而将Vue.js视为一个意见较少的框架。

综上所述 (In summary)

  • Frameworks and libraries are both code written by someone else that helps you perform some common tasks in a less verbose way.

    框架和库都是由其他人编写的代码,可以帮助您以不太冗长的方式执行一些常见任务。
  • A framework inverts the control of the program. It tells the developer what they need. A library doesn’t. The programmer calls the library where and when they need it.

    框架会反转程序的控制权。 它告诉开发人员他们需要什么。 图书馆没有。 程序员调用库在何时何需要它。

  • The degree of freedom a library or framework gives the developer will dictate how “opinionated” it is.

    库或框架给开发人员的自由度将决定它的“意见”程度。

Thanks for reading!

谢谢阅读!

翻译自: https://www.freecodecamp.org/news/the-difference-between-a-framework-and-a-library-bd133054023f/

框架和库的区别

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/392047.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java—多线程实现生产者消费者模型

采用线程实现“生产者-消费者”编程的基础模型 源代码 消费者代码&#xff1a; public class Consumer implements Runnable {BlockingQueue<Integer> blockingQueue;int n;CountDownLatch countDownLatch;public Consumer(BlockingQueue<Integer> blockingQueue…

动态链接库.so和静态链接库.a的区别

静态链接库&#xff1a; •扩展名&#xff1a;.a  •编译行为&#xff1a;在编译的时候&#xff0c;将函数库直接整合到执行程序中&#xff08;所以利用静态库编译生成的文档会更大&#xff09; •独立执行的状态&#xff1a;编译成功的可执行文件可以独立运行&#xff0c;不…

华为鸿蒙系统封闭,谷歌正式“除名”华为!“亲儿子”荣耀表示:暂不考虑,鸿蒙OS处境尴尬...

我们都知道&#xff0c;目前智能手机最常用操作系统就是IOS和安卓&#xff0c;占据手机系统超过99%的市场份额。由于IOS系统的封闭性&#xff0c;国内手机厂商基本上都是使用谷歌的开源安卓系统。当然华为也不例外&#xff0c;一直使用的都是安卓系统。可以说&#xff0c;安卓系…

使用vue-cli脚手架搭建简单项目框架

1.首先已经安装了node,最好版本6以上。 2.安装淘宝镜像 大家都知道国内直接使用 npm 的官方镜像是非常慢的&#xff0c;这里推荐使用淘宝 NPM 镜像。这样就可以直接使用cnpm了。 npm install -g cnpm --registryhttps://registry.npm.taobao.org如果过程出差&#xff0c;是否安…

sap中泰国有预扣税设置吗_泰国餐厅密度细分:带有K-means聚类的python

sap中泰国有预扣税设置吗Hi! I am Tung, and this is my first stories for my weekend project. What inspired this project is that I have studied to become data scientist for almost two years now mostly from Youtube, coding sites and of course, Medium ,but my l…

自动化yaml文件_从YAML到TypeScript:开发人员对云自动化的看法

自动化yaml文件The rise of managed cloud services, cloud-native, and serverless applications brings both new possibilities and challenges. More and more practices from software development processes like version control, code review, continuous integration,…

SQL SERVER-Extendevent系统视图

--获得扩展事件的事件select name,description from sys.dm_xe_objects where object_typeevent order by name--获得各事件的字段 select c.name,c.description from sys.dm_xe_object_columns c inner join sys.dm_xe_objects o on o.namec.object_name where o.name…

Java—简单的注册页面

根据所提供的界面&#xff0c;编写 register.html 文件 源代码 empty.jsp <% page contentType"text/html;charsetUTF-8" language"java" %> <html> <head><title>error</title> </head> <body> <H1><…

【深度学习系列】用PaddlePaddle和Tensorflow实现经典CNN网络AlexNet

上周我们用PaddlePaddle和Tensorflow实现了图像分类&#xff0c;分别用自己手写的一个简单的CNN网络simple_cnn和LeNet-5的CNN网络识别cifar-10数据集。在上周的实验表现中&#xff0c;经过200次迭代后的LeNet-5的准确率为60%左右&#xff0c;这个结果差强人意&#xff0c;毕竟…

图片获取像素坐标html,HTML5画布Canvas图片抽取、像素信息获取、命中检测

今天主要介绍canvas中比较强大的功能比如将画布内容抽取为图片获取、修改画布的像素信息以及画布的命中检测首先我仍然需要创建画布图片抽取首先要明确的一点是toDataURL()是canvas对象自身的方法而不是环境对象的这个方法会将canvas的内容抽取为一张图片(base64编码)我们来看一…

CentOS6 下Samba服务器的安装与配置

原地址&#xff1a;http://www.cnblogs.com/mchina/archive/2012/12/18/2816717.html 一、简介 Samba是一个能让Linux系统应用Microsoft网络通讯协议的软件&#xff0c;而SMB是Server Message Block的缩写&#xff0c;即为服务器消息块 &#xff0c;SMB主要是作为Microsoft的网…

傅里叶变换 直观_A / B测试的直观模拟

傅里叶变换 直观Many of us have heard, read, or even performed an A/B Test before, which means we have conducted a statistical test at some point. Most of the time, we have worked with data from first or third-party sources and performed these tests with ea…

tableau for循环_Tableau for Data Science and Data Visualization-速成课程

tableau for循环Tableau is software that can help you see and understand your data. It is used for data science and data visualization. Tableau allows you to connect to almost any database, drag and drop to create visualizations, and share with a click.Tabl…

请求接口时使用时间戳

&tnew Date().getTime()转载于:https://www.cnblogs.com/Glant/p/11271960.html

Java—servlet简单使用

【步骤 1】创建一个名为 input.html 的 HTML 页面&#xff0c;其中包括一个表单&#xff0c;表单中包含两 个文本域&#xff0c;分别供用户输入学号和姓名&#xff0c;该页面也包含提交和重置按钮。 【步骤 2】定义一个名为 com.demo.Student 类&#xff0c;其中包括学号 sno 和…

phpstrom+phpstudy+postman

1.打开phpstudy xdebug 扩展 2.修改php.ini [XDebug]xdebug.profiler_output_dir"D:\phpStudy\tmp\xdebug"xdebug.trace_output_dir"D:\phpStudy\tmp\xdebug"zend_extension"D:\phpStudy\php\php-5.5.38\ext\php_xdebug.dll";是否允许Xdebug跟踪…

SIP协议

SIP协议 SIP协议主要包括 SIP头 SIP内容 和附加内容三个部分 项目格式备注示例SIP头一行&#xff0c;以\r\n结尾REGISTER sip:172.30.2.35 SIP/2.0\r\nSIP内容很多行&#xff0c;每行为Key&#xff0c;Value的形式CSeq: 1 REGISTER\r\n附加内容很多行1 SIP头 项目格式含义示例I…

android emmc 命令,使用CoreELEC的ceemmc工具将系统写入emmc

最近在折腾电视盒子&#xff0c;CoreELEC是专门为晶晨CPU开发系统&#xff0c;个人觉的非常不错&#xff0c;相关资料可以百度。这里介绍将卡载系统刷入emmc内置存储的方法。因为找不到相关的教程&#xff0c;只在官网上找到了ceemmc这个工具的使用说明&#xff0c;所以搬过来。…

ios 自定义字体_如何仅用几行代码在iOS应用中创建一致的自定义字体

ios 自定义字体by Yuichi Fujiki藤木雄一 In this article, youll learn how to create a unified custom look throughout your app with these simple tricks.在本文中&#xff0c;您将学习如何使用这些简单的技巧在整个应用程序中创建统一的自定义外观。 我们想做什么 (Wh…

truncate 、delete与drop区别

相同点&#xff1a; 1.truncate和不带where子句的delete、以及drop都会删除表内的数据。 2.drop、truncate都是DDL语句(数据定义语言),执行后会自动提交。 不同点&#xff1a; 1. truncate 和 delete 只删除数据不删除表的结构(定义)drop 语句将删除表的结构被依赖的约束(const…