报错找不到原因,
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>ES6图片切换</title>
</head>
<body>
<div id="wrap">
</div>
<script type="text/javascript">
/////////////////********data.js******/////////////////
const data = [
{
type: 'JavaScript',
title: 'ES6快速入门',
alt: 'ES6快速入门',
src: 'images/1.jpg'
},
{
type: 'JavaScript',
title: 'Javascript实现二叉树算法',
alt: 'Javascript实现二叉树算法',
src: 'images/2.jpg'
},
{
type: 'JavaScript',
title: 'Canvas绘制时钟',
alt: 'Canvas绘制时钟',
src: 'images/3.jpg'
},
{
type: 'JavaScript',
title: '基于websocket的火拼俄罗斯',
alt: '基于websocket的火拼俄罗斯',
src: 'images/15.jpg'
},
{
type: '前端框架',
title: 'React知识点综合运用实例',
alt: 'React知识点综合运用实例',
src: 'images/4.jpg'
},
{
type: '前端框架',
title: 'React组件',
alt: 'React组件',
src: 'images/5.jpg'
},
{
type: '前端框架',
title: 'Vue+Webpack打造todo应用',
alt: 'Vue+Webpack打造todo应用',
src: 'images/6.jpg'
},
{
type: '前端框架',
title: 'Vue.js入门基础',
alt: 'Vue.js入门基础',
src: 'images/7.jpg'
},
{
type: '前端框架',
title: '使用Vue2.0实现购物车和地址选配功能',
alt: '使用Vue2.0实现购物车和地址选配功能',
src: 'images/8.jpg'
},
{
type: 'React',
title: 'React知识点综合运用实例',
alt: 'React知识点综合运用实例',
src: 'images/4.jpg'
},
{
type: 'React',
title: 'React组件',
alt: 'React组件',
src: 'images/5.jpg'
},
{
type: 'Vue.js',
title: 'Vue+Webpack打造todo应用',
alt: 'Vue+Webpack打造todo应用',
src: 'images/6.jpg'
},
{
type: 'Vue.js',
title: 'Vue.js入门基础',
alt: 'Vue.js入门基础',
src: 'images/7.jpg'
},
{
type: 'Vue.js',
title: '使用Vue2.0实现购物车和地址选配功能',
alt: '使用Vue2.0实现购物车和地址选配功能',
src: 'images/8.jpg'
}
];
/////////////////********data.js******/////////////////
/////////////////********index.js******/////////////////
///1.对图片分类
///2.生成DOM元素
///3.绑定事件
///4.显示到页面上
(function(window,document){
const methods = {
eppendChild(parent,...children){
children.forEach(el =>{
parent.appendChild(el);
});
},
$(selector,root = document){
return root.querySelector(selector);
},
$$(selector,root = document){
return root.querySelectorAll(selector);
}
};
let Img = function(option){
this._init(option);
this._createElement();
this._bind();
this._show();
};
Img.prototype._init = function(data,initType,dom){
this.types = ['全部'];//所有分类
this.all = [];//所有图片元素
this.classified = {'全部':[]};//按照类型分类后的图片
this.currentType = initType;//页面显示的当前图片分类
this.dom = methods.$(dom);//挂载点
this._classify(data);
console.log(this.all);
console.log(this.dom);
console.log(this.classified);
};
Img.prototype._classify = function(data){
//类型1 类型2 类型3 类型2 类型3
//0 1 2 3 4 --index
// classified:{
// '类型 1':[0], --index
// '类型2':[1,3], --index
// '类型3':[2,4] --index
// }
let srcs = [];
data.forEach(function({title,type,alt,src}){
if(!this.types.inculdes(type)){
this.types.push(type);
}
if(!Object.keys(this.classified).includes(type)){
this.classified[type] = [];
}
if(!srcs.includes(src)){
//图片没有生成过
//生成图片
//添加到对应的分类中
srcs.push(src);
let figure = document.createElement('figure');
let img = document.createElement('img');
let figcaption = document.createElement('figcaption');
img.src = src;
img.setAttribute('alt',alt);
figcaption.innerText = title;
methods.appendChild(figure,img,figcaption);
this.all.push(figure);
this.classified[type].push(this.all.length-1);
}else{
//去all中找到对用的图片
//添加到对应的分类中
this.classified[type].push(srcs.findIndex(s1 => s1 === src))
}
});
};
Img.prototype._createElement = function(){
};
Img.prototype._bind = function(){
};
Img.prototype._show = function(){
};
window.$Img = Img;
})(window,document)
/////////////////********index.js******/////////////////
const img = new $Img();
</script>
</body>
</html>36
收起
正在回答
4回答
同学你好,因为使用普通函数的话,this指向的是window(在普通函数中的this总是代表它的直接调用者,在默认情况下,this指的是window)
而这里使用箭头函数的话,this指的是当前这个实例(箭头函数没有自己的this, 它的this是继承而来; 默认指向在定义它时所处的对象(宿主对象))
所以,这里需要使用箭头函数去实现。
另,代码中其他问题:单词写错


如果我的回答帮到了你,欢迎采纳,祝学习愉快~
RahodJoe
2020-04-30 11:00:27
老师,我实例化传参的时候,forEach又报错了,你们帮忙看看,谢谢了
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>ES6图片切换</title>
</head>
<body>
<div id="wrap">
</div>
<script type="text/javascript">
/////////////////********data.js******/////////////////
const data = [
{
type: 'JavaScript',
title: 'ES6快速入门',
alt: 'ES6快速入门',
src: 'images/1.jpg'
},
{
type: 'JavaScript',
title: 'Javascript实现二叉树算法',
alt: 'Javascript实现二叉树算法',
src: 'images/2.jpg'
},
{
type: 'JavaScript',
title: 'Canvas绘制时钟',
alt: 'Canvas绘制时钟',
src: 'images/3.jpg'
},
{
type: 'JavaScript',
title: '基于websocket的火拼俄罗斯',
alt: '基于websocket的火拼俄罗斯',
src: 'images/15.jpg'
},
{
type: '前端框架',
title: 'React知识点综合运用实例',
alt: 'React知识点综合运用实例',
src: 'images/4.jpg'
},
{
type: '前端框架',
title: 'React组件',
alt: 'React组件',
src: 'images/5.jpg'
},
{
type: '前端框架',
title: 'Vue+Webpack打造todo应用',
alt: 'Vue+Webpack打造todo应用',
src: 'images/6.jpg'
},
{
type: '前端框架',
title: 'Vue.js入门基础',
alt: 'Vue.js入门基础',
src: 'images/7.jpg'
},
{
type: '前端框架',
title: '使用Vue2.0实现购物车和地址选配功能',
alt: '使用Vue2.0实现购物车和地址选配功能',
src: 'images/8.jpg'
},
{
type: 'React',
title: 'React知识点综合运用实例',
alt: 'React知识点综合运用实例',
src: 'images/4.jpg'
},
{
type: 'React',
title: 'React组件',
alt: 'React组件',
src: 'images/5.jpg'
},
{
type: 'Vue.js',
title: 'Vue+Webpack打造todo应用',
alt: 'Vue+Webpack打造todo应用',
src: 'images/6.jpg'
},
{
type: 'Vue.js',
title: 'Vue.js入门基础',
alt: 'Vue.js入门基础',
src: 'images/7.jpg'
},
{
type: 'Vue.js',
title: '使用Vue2.0实现购物车和地址选配功能',
alt: '使用Vue2.0实现购物车和地址选配功能',
src: 'images/8.jpg'
}
];
/////////////////********data.js******/////////////////
/////////////////********index.js******/////////////////
///1.对图片分类
///2.生成DOM元素
///3.绑定事件
///4.显示到页面上
(function(window,document){
const methods = {
appendChild(parent,...children){
children.forEach(el =>{
parent.appendChild(el);
});
},
$(selector,root = document){
return root.querySelector(selector);
},
$$(selector,root = document){
return root.querySelectorAll(selector);
}
};
//构造函数
let Img = function(option){
this._init(option);
this._createElement();
this._bind();
this._show();
};
//初始化
Img.prototype._init = function(data,initType,dom){
this.types = ['全部'];//所有分类
this.all = [];//所有图片元素
this.classified = {'全部':[]};//按照类型分类后的图片
this.currentType = initType;//页面显示的当前图片分类
this.dom = methods.$(dom);//挂载点
this._classify(data);
// console.log(this.classified);
};
//对图片分类
Img.prototype._classify = function(data){
//类型1 类型2 类型3 类型2 类型3
//0 1 2 3 4 --index
// classified:{
// '类型 1':[0], --index
// '类型2':[1,3], --index
// '类型3':[2,4] --index
// }
let srcs = [];
data.forEach(({title,type,alt,src}) => {
if(!this.types.includes(type)){
this.types.push(type);
}
if(!Object.keys(this.classified).includes(type)){
this.classified[type] = [];
}
if(!srcs.includes(src)){
//图片没有生成过
//生成图片
//添加到对应的分类中
srcs.push(src);
let figure = document.createElement('figure');
let img = document.createElement('img');
let figcaption = document.createElement('figcaption');
img.src = src;
img.setAttribute('alt',alt);
figcaption.innerText = title;
methods.appendChild(figure,img,figcaption);
this.all.push(figure);
this.classified[type].push(this.all.length-1);
}else{
//去all中找到对用的图片
//添加到对应的分类中
this.classified[type].push(srcs.findIndex( s1 => s1 === src));
}
});
};
//生成DOM元素
Img.prototype._createElement = function(){
let typesBtn = [];
for(let type of this.types){
typesBtn.push(`<li class="__Img_classify__type-btn${this.currentType === type ? ' __Img__type-btn-active' : ''}">${type}</li>`);
}
// console.log(typesBtn);
};
//绑定事件
Img.prototype._bind = function(){
};
//显示元素
Img.prototype._show = function(){
};
window.$Img = Img;
})(window,document)
/////////////////********index.js******/////////////////
const img = new $Img({
data,
initType:'JavaScript',
dom:'#wrap'
});
</script>
</body>
</html>
4.Vue与React高级框架开发
- 参与学习 人
- 提交作业 239 份
- 解答问题 10739 个
本阶段带你深入前端开发的肌理,通过ES6基础知识和前端主流高级框架的学习,助你快速构建企业级移动webAPP应用,进入职场的终极battle
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星