框架和庫的區別_框架和庫之間的區別

框架和庫的區別

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.pswp.cn/news/392047.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392047.shtml
英文地址,請注明出處:http://en.pswp.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…