什么定义了 JavaScript 中的对象长度
what defines an object length in javascript?
function CookieStorage(maxage, path) { // Arguments specify lifetime and scope
// Get an object that holds all cookies
var cookies = (function() { // The getCookies() function shown earlier
var cookies = {}; // The object we will return
var all = document.cookie; // Get all cookies in one big string
if (all === "") // If the property is the empty string
return cookies; // return an empty object
var list = all.split("; "); // Split into individual name=value pairs
for(var i = 0; i < list.length; i++) { // For each cookie
var cookie = list[i];
var p = cookie.indexOf("="); // Find the first = sign
var name = cookie.substring(0,p); // Get cookie name
var value = cookie.substring(p+1); // Get cookie value
value = decodeURIComponent(value); // Decode the value
cookies[name] = value; // Store name and value
}
return cookies;
}());
// Collect the cookie names in an array
var keys = [];
for(var key in cookies) keys.push(key);
// Now define the public properties and methods of the Storage API
// The number of stored cookies
**this.length = keys.length;**
// Return the name of the nth cookie, or null if n is out of range
this.key = function(n) {
if (n < 0 || n >= keys.length) return null;
return keys[n];
};
// Return the value of the named cookie, or null.
this.getItem = function(name) { return cookies[name] || null; };
**// Store a value
this.setItem = function(key, value) {
if (!(key in cookies)) { // If no existing cookie with this name
keys.push(key); // Add key to the array of keys
this.length++; // And increment the length
}**
// Store this name/value pair in the set of cookies.
cookies[key] = value;
// Now actually set the cookie.
// First encode value and create a name=encoded-value string
var cookie = key + "=" + encodeURIComponent(value);
// Add cookie attributes to that string
if (maxage) cookie += "; max-age=" + maxage;
if (path) cookie += "; path=" + path;
// Set the cookie through the magic document.cookie property
document.cookie = cookie;
};
大家好,我在我正在阅读的一本书中找到了这段代码,我看到了这句对我来说毫无意义的行:
**// Store a value
this.setItem = function(key, value) {
if (!(key in cookies)) { // If no existing cookie with this name
keys.push(key); // Add key to the array of keys
this.length++; // And increment the length
}**
如果我们当前所在的对象 length 属性已经被前一行代码定义(this.length = keys.length;(为什么我们需要通过 this.length++ 来增加它的长度?key.push(key(还不够吗?
编辑:
感谢所有回答的人。在盯着这段代码几分钟后,我发现第一个长度声明只与脚本时间中的那个"阶段"相关。this.length = keys.length表示此对象长度等于键数组的当前长度。
后来,当我们向键数组添加另一个元素时,它的长度增加了,这就是为什么我们必须通过这次增加它自己的值来告诉它给我们的对象(this.length++;(
这就足够了,但是如果你想从外部代码使用这个函数,那么你就无法访问内部values
数组,所以这是一个方便的属性来获取长度。
您可以像这样用 getter 替换它,并且每次都跳过手动更新它:
Object.defineProperty(this, "length", {
get: function() {
return keys.length;
}
});
注意:正如@zzzzBov在他的评论中指出的那样,IE8及更低版本尚不支持getter。
this.setItem = function(key, value) {
if (!(key in cookies)) { // If no existing cookie with this name
keys.push(key); // Add key to the array of keys
this.length++; // And increment the length
}
...
};
如果你只看这段代码,你会注意到它声明了一个函数。此函数中的代码将在 CookieStorage
的实例上调用 setItem
时执行。
创建CookieStorage
实例时调用上一行。增加长度的目的是为私有存储数组的长度生成公共 API。
的第一个答案。希望对您有所帮助。
看起来像这样.长度并不意味着 cookie 数组的长度,不是吗?是的,如果它使用 push(( 函数,数组的长度肯定会增加 1。您不必手动执行此操作。
除非,这个长度意味着别的东西。
对于您的第一个问题:基本上,它定义了函数仅在您请求函数长度时才获得的参数数量,例如: CookieStorage.length
function a() {}
function b(a, b) {}
function c(a,b,c) {}
console.log(a.length); //output 0
console.log(b.length); //output 2
console.log(c.length); //output 3
对于this.length
问题:好吧,this.length 是持有keys.length
的属性。因此,当您将新项设置为 keys 数组时,它会使用新长度更新 length 属性。因此,您可以将其用于:
var cookies = new CookieStorage();
cookies.setItem("test","test");
cookies.length //output 1
- 在Javascript服务器/客户端中共享对象定义
- 高级布线和单个对象定义
- Javascript 对象定义
- 是否可以为数组中的每个对象定义事件侦听器
- 如何在多个文件中拆分 JavaScript 对象定义
- 将对象定义为仅包含特定类型的元素
- 如何在 javascript 中为 dom 对象定义函数
- 您可以在 JavaScript 中的私有方法中访问对象定义的属性吗?
- 在 ecma-script-6 中将对象定义为常量
- 在 JavaScript 中使用对象定义局部变量
- 如何在 JS 对象定义中引用字段
- 属性的对象定义属性设置器
- Three.js对象定义未定义
- 对象定义有两种方式-有什么区别
- JavaScript-如何在对象定义中定义属性
- 对象定义中的方括号表示法
- 仅为{..}对象定义原型
- 在对象定义中的javascript Repeating属性中(在大括号内).这是个错误吗
- 特殊的JavaScript结构:对象定义中的对象类型变量
- 在javascript对象定义中连接字符串