如何在使用之前将初始数据赋值给文字对象中的一些变量
How can I assign initial data to some variables in literal object before use
我有一个做某事的文字对象,这个对象必须在使用它的任何方法之前分配一些初始数据给变量。
var slideshow = {
slideshowBox: null,
slideImages: null,
slideImagesLen: 0,
init: function(){
this.slideshowBox = document.getElementById('slideshow');
this.slideImages = document.getElementById('images');
this.slideImagesLen = this.slideImages.children.length;
},
show: function(){
return this.slideImagesLen;
}
};
但问题是,当使用它的任何方法时,你必须首先使用init
方法,然后使用我可以使用任何其他方法,但这种行为是不好的。
slideshow.init()
console.log(slideshow.show());
我也试着用下面的方法:
(function(){
var slideshow = {
slideshowBox: null,
slideImages: null,
slideImagesLen: 0,
init: function(){
this.slideshowBox = document.getElementById('slideshow');
this.slideImages = document.getElementById('images');
this.slideImagesLen = this.slideImages.children.length;
},
show: function(){
return this.slideImagesLen;
}
};
slideshow.init();
})();
但是,this.slideImages is null
, slideshow.show is not a function
我想在使用任何方法之前自动调用init
方法,而不需要手动调用它。
你应该尝试在构造函数中使用原型继承。
(function(){
var Slideshow = function () {
this.slideshowBox = document.getElementById('slideshow');
this.slideImages = document.getElementById('images');
this.slideImagesLen = this.slideImages.children.length;
};
Slideshow.prototype.show = function () {
return this.slideImagesLen;
};
var slideShowInstance = new Slideshow();
slideShowInstance.show();
})();
实际上我还会做得更深入一些使它更具可扩展性
var Slideshow = function (slideShowId, images) {
this.slideshowBox = slideShowId;
this.slideImages = images;
this.slideImagesLen = this.slideImages.children.length;
};
var slideShowId = document.getElementById('slideshow'),
images = document.getElementById('images');
var slideShowInstance = new Slideshow(slideShowId, images);
尝试在函数slides
中调用_slideshow.init
后返回Object
_slideshow
var slides = function(options) {
return (function(opts) {
var _slideshow = opts || {
slideshowBox: null,
slideImages: null,
slideImagesLen: 0,
init: function() {
this.slideshowBox = document.getElementById('slideshow');
this.slideImages = document.getElementById('images');
this.slideImagesLen = this.slideImages.children.length;
},
show: function() {
return this.slideImagesLen;
}
};
_slideshow.init();
return Object.create(_slideshow)
}(options))
};
var slideshow = slides();
console.log("slideImagesLen:", slideshow.show(), "slideshow:", slideshow);
<div id="slideshow">
<div id="images">
<img src="http://lorempixel.com/50/50/nature" />
</div>
</div>
有几种方法。例如:
var slideshow = {
...
init: function () {
...
return this;
}
...
}.init();
或者如果仍然太手动,您可以简单地在对象文字中使用IIFE:
var slideshow = {
...
init: (function init () {
...
return init;
}())
...
}
在这两种情况下,引用的HTML元素必须在创建对象之前存在。
相关文章:
- 如何计算对象文字中的键
- 对象文字方法上的Javascript绑定不起作用
- 可以使用“;创建“;作为JavaScript对象文字中的属性名称
- 这两种不同的创建对象文字的方法有什么区别
- Javascript对象文字,POST问题
- 为什么可以'我们在函数体中为函数对象添加属性,就像在javascript中为对象文字添加属性一样
- 如何使用'这个“;对象文字中的关键字
- JS对象文字中的方法和子方法
- 向JavaScript对象文字添加数据
- Javascript对象文字,如何使用“this”来引用对象中的变量
- 如何将xml文件转换为javascript中的对象文字
- 什么's对象文字中带引号和不带引号的键之间的区别
- 使用窗口对象练习我的对象文字函数
- 对象文字数据表中的 Google 可视化样式角色
- 如何创建作用域的动态、基于实例的对象文字
- Javascript将字符串中的对象转换为对象文字
- 我需要一些帮助来理解JavaScript中的对象文字函数
- 为什么动画设置加载在TweenMax中的两个单独的对象文字中
- 对象文字语法怪异
- 对象文字中的类