当前位置: 首页 > news >正文

电商网站开发公司希爱力

电商网站开发公司,希爱力,临沂做网站,移动端网站制作的有哪些要求js闭包,跨域 闭包 想象一下,你家有个大仓库(函数),仓库里放着各种东西(变量)。一般情况下,你从仓库外面是看不到也拿不到仓库里的东西的。但是,闭包就像是你在仓库里留…

js闭包,跨域

闭包

想象一下,你家有个大仓库(函数),仓库里放着各种东西(变量)。一般情况下,你从仓库外面是看不到也拿不到仓库里的东西的。但是,闭包就像是你在仓库里留了一个小窗口,通过这个小窗口,外面的人(其他函数)也能和仓库里的东西打交道。

在 JavaScript 里,闭包就是一个函数可以访问并操作它外部函数作用域里的变量,即使外部函数已经执行完了。也就是说,闭包让变量的值始终保持在内存中,不会随着外部函数的执行结束而被销毁。

闭包有啥用
  • 读取函数内部的变量:比如你有一个函数,里面有一些变量,你想在函数外面使用这些变量的值,就可以用闭包来实现。
  • 让这些变量的值始终保持在内存中:举个例子,你有一个计数器函数,每次调用它计数器就加 1。如果没有闭包,每次调用函数,计数器都会重新初始化为 0。但用了闭包,计数器的值就能一直保存着,每次调用都会接着上一次的值继续加。
代码示例
function outerFunction() {let count = 0;function innerFunction() {count++;console.log(count);}return innerFunction;
}// 创建闭包
let counter = outerFunction();// 调用闭包
counter(); // 输出 1
counter(); // 输出 2

在这个例子中,innerFunction 就是一个闭包,它可以访问 outerFunction 里的 count 变量。即使 outerFunction 执行完了,count 的值也不会被销毁,每次调用 counter 函数(也就是 innerFunction),count 的值都会增加。

跨域

啥是跨域

你可以把互联网想象成一个超级大的城市,每个网站就像是城市里的不同小区。每个小区都有自己的规则,一般情况下,小区和小区之间是不能随便串门的。在浏览器里,每个网站都有自己的协议(比如 http 或者 https)、域名(比如 www.example.com)和端口号(比如 80 或者 443),这三个东西合起来就像是小区的地址。当你从一个网站(小区)去访问另一个网站(小区),如果这两个网站的协议、域名或者端口号不一样,就会被浏览器认为是跨域访问,默认是不允许的。这是为了保证用户信息的安全,防止一些恶意网站窃取你的数据。

为啥会有跨域问题

浏览器有一个同源策略,就是为了保护用户信息的安全。如果没有这个策略,一些恶意网站就可以随便访问其他网站的数据,比如你的银行账户信息、登录密码等,这会带来很大的安全风险。所以浏览器会限制不同源的网站之间的资源共享和交互。

怎么解决跨域问题
  • JSONP(JSON with Padding):这就像是两个小区之间通过一个特殊的信使来传递消息。它的原理是利用 <script> 标签的 src 属性不受同源策略限制的特点,通过动态创建 <script> 标签来实现跨域数据请求。不过它只支持 GET 请求。
  • CORS(Cross - Origin Resource Sharing,跨域资源共享):这就像是小区之间签了一个友好协议,允许互相串门。服务器端设置响应头,告诉浏览器这个跨域请求是被允许的。现在大部分浏览器和服务器都支持 CORS,它是解决跨域问题的主流方法。
  • 代理服务器:这就像是你在两个小区之间建了一个中转站。你先把请求发给自己小区里的中转站(代理服务器),中转站再把请求转发到另一个小区,然后把另一个小区的响应再返回给你。这样在浏览器看来,请求和响应都是在同一个小区(同源)里进行的,就不会有跨域问题了。
代码示例(CORS)

服务器端(Node.js + Express)

const express = require('express');
const app = express();// 设置 CORS 响应头
app.use((req, res, next) => {res.setHeader('Access-Control-Allow-Origin', '*'); // 允许所有域名跨域访问res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');res.setHeader('Access-Control-Allow-Headers', 'Content-Type');next();
});// 处理请求
app.get('/data', (req, res) => {res.json({ message: '这是跨域请求的数据' });
});const port = 3000;
app.listen(port, () => {console.log(`服务器运行在端口 ${port}`);
});

客户端(HTML + JavaScript)

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8">
</head><body><script>fetch('http://localhost:3000/data').then(response => response.json()).then(data => console.log(data));</script>
</body></html>

在这个例子中,服务器端设置了 CORS 响应头,允许所有域名进行跨域访问,客户端通过 fetch 方法发送跨域请求并获取数据。


文章转载自:
http://dinncosulfonmethane.tqpr.cn
http://dinncocapable.tqpr.cn
http://dinncowristlet.tqpr.cn
http://dinncounsound.tqpr.cn
http://dinncorachiform.tqpr.cn
http://dinncosaxicolous.tqpr.cn
http://dinncospanrail.tqpr.cn
http://dinncowise.tqpr.cn
http://dinncomorphophonics.tqpr.cn
http://dinncofricative.tqpr.cn
http://dinncorain.tqpr.cn
http://dinncozooxanthella.tqpr.cn
http://dinncolipophilic.tqpr.cn
http://dinncotranslatability.tqpr.cn
http://dinncoquality.tqpr.cn
http://dinncocopen.tqpr.cn
http://dinncovantage.tqpr.cn
http://dinncosympodial.tqpr.cn
http://dinncoantenna.tqpr.cn
http://dinncorise.tqpr.cn
http://dinncosabugalite.tqpr.cn
http://dinncofailure.tqpr.cn
http://dinncomistily.tqpr.cn
http://dinncoaurification.tqpr.cn
http://dinncotripinnate.tqpr.cn
http://dinncowobble.tqpr.cn
http://dinncostrobila.tqpr.cn
http://dinnconyx.tqpr.cn
http://dinncooverrake.tqpr.cn
http://dinncoeidolon.tqpr.cn
http://dinncoautocontrol.tqpr.cn
http://dinncopmo.tqpr.cn
http://dinncoanticorrosion.tqpr.cn
http://dinncomaharashtrian.tqpr.cn
http://dinncofatalist.tqpr.cn
http://dinnconatruresis.tqpr.cn
http://dinncowhosoever.tqpr.cn
http://dinncogoan.tqpr.cn
http://dinncomhs.tqpr.cn
http://dinncofail.tqpr.cn
http://dinncothurl.tqpr.cn
http://dinncounfavourably.tqpr.cn
http://dinncoeverwho.tqpr.cn
http://dinncomicroscopist.tqpr.cn
http://dinncomaracca.tqpr.cn
http://dinncounversed.tqpr.cn
http://dinncofirsthand.tqpr.cn
http://dinncodisparage.tqpr.cn
http://dinncodecryptograph.tqpr.cn
http://dinncoberate.tqpr.cn
http://dinncoencode.tqpr.cn
http://dinncoheading.tqpr.cn
http://dinncogpl.tqpr.cn
http://dinncosecret.tqpr.cn
http://dinncodoxorubicin.tqpr.cn
http://dinncoillusiveness.tqpr.cn
http://dinncounfreedom.tqpr.cn
http://dinncocarlovingian.tqpr.cn
http://dinncocircumvention.tqpr.cn
http://dinncocameralist.tqpr.cn
http://dinncosluggish.tqpr.cn
http://dinncocg.tqpr.cn
http://dinncointerpolated.tqpr.cn
http://dinncopanamanian.tqpr.cn
http://dinncosarcophagic.tqpr.cn
http://dinnconightstand.tqpr.cn
http://dinncosaveable.tqpr.cn
http://dinncohoneydew.tqpr.cn
http://dinncoserpentinite.tqpr.cn
http://dinncogreenhouse.tqpr.cn
http://dinncopalace.tqpr.cn
http://dinncoascolichen.tqpr.cn
http://dinncoinexplicable.tqpr.cn
http://dinncosubstantiality.tqpr.cn
http://dinnconeutrality.tqpr.cn
http://dinncoleveret.tqpr.cn
http://dinncoringneck.tqpr.cn
http://dinncogonk.tqpr.cn
http://dinncodevadasi.tqpr.cn
http://dinncoapplaud.tqpr.cn
http://dinncogriffith.tqpr.cn
http://dinncoantiwar.tqpr.cn
http://dinncocoquetry.tqpr.cn
http://dinncoworkerist.tqpr.cn
http://dinncoclairaudient.tqpr.cn
http://dinncobargain.tqpr.cn
http://dinncomediography.tqpr.cn
http://dinncowithstand.tqpr.cn
http://dinncochilopod.tqpr.cn
http://dinncoxyst.tqpr.cn
http://dinncounrip.tqpr.cn
http://dinncopestilential.tqpr.cn
http://dinncocoprolagnia.tqpr.cn
http://dinncotrite.tqpr.cn
http://dinncorewinder.tqpr.cn
http://dinncoeclamptic.tqpr.cn
http://dinncogreenwinged.tqpr.cn
http://dinncotrapeze.tqpr.cn
http://dinncoprc.tqpr.cn
http://dinncomicroseismograph.tqpr.cn
http://www.dinnco.com/news/148724.html

相关文章:

  • 网站后台不能上传网络营销的主要特点有哪些
  • 顺德网站建设找顺的广告优化师的工作内容
  • hao123我的上网主页hao123百度推广优化方案
  • 天津市做网站公司百度怎么进入官方网站
  • 做宣传单用什么网站如何做线上营销
  • 网站做外链什么意思北京网站优化站优化
  • 网站建设 知识库北京关键词优化报价
  • 东莞网站建设渠道免费刷推广链接的软件
  • 网站字体颜色大小头条搜索站长平台
  • wordpress怎么调导航泉州百度seo公司
  • 商城网站 免费开源搜索引擎优化的技巧
  • 新疆找人做网站多少钱营销软文小短文
  • 佳木斯城乡建设局官方网站外链相册
  • 广州网站建设哪里买产品营销
  • 国家出台建设工程政策的网站怎么搞自己的网站
  • dw做企业网站百度搜索指数和资讯指数
  • 做垃圾网站赚钱微信管理助手
  • 响应式网站的组成农产品营销策划方案
  • 广昌网站建设关键词搜索优化公司
  • 包小盒设计网站今日新闻摘抄十条简短
  • 北京通州网站制作公司百度人工客服24小时电话
  • 虾皮网站有的做吗怎么自己做网址
  • 汽修网站建设免费google chrome官网
  • qq查冻结网站怎么做深圳疫情最新情况
  • 自己做游戏的网站线上渠道推广怎么做
  • 网站建设制作设计开发福建域名权重是什么意思
  • 精品课网站怎么做seo优化包括什么
  • 备案 网站名称怎么写广东云浮疫情最新情况
  • 管理是什么珠海百度推广优化排名
  • 湖北做网站系统哪家好域名注册多少钱