prototype与几个循环的心得

《一》prototype
prototype其实是函数的一个属性,并且只有函数有这个属性,这个属性就是给函数增加函数或者属性的,比如写一个
function one(){},那么one.prototype的类型就是一个对象,使用JSON.stringify(one.prototype)得到的就是一个字符串类型的{}了,显然是一个空的对象。
我添加一个name属性试试,one.prototype.name="这是文本",再JSON.stringify(one.prototype)得到:{‘name’:'这是文本'}。
一个函数其实也是可以变成一个对象的,通过new 函数名 变成对象,这样就可以访问函数里边的对象或者属性了。
我来举个函数添加属性和函数的例子:
function one(){
this.index=1;
}
one.prototype.two=function(e){
return this.index;//返回one里边的index值1,这里的this是one
//return {value:this.index,name:e}//返回的是一个对象
}
var test=new one();//函数对象化
test.hasOwnProperty('index')//判断是否有index这个属性
test;//object{index:1,prototype:two}
test.index;//1
test.two;//返回two函数
test.two();//返回运行后的函数结果,里边是this.index值为1

还有一种写法,是对one进行扩展的,如下:
function one(){
this.index=1;
}
one.prototype=function(){
describe:'version1',
two:function(){//因为two在这里是种对象里的方法,所以写成这样,很多封装好的库里都是这样,比如jQuery
return this.index;
}
}
var test=new one();
test.two();
《二》循环遍历
这个遍历数组或者对象,首先得到的是索引或者是key值,比如
var arr=[3,4,1];
for(var index in arr){
console.log(index)//1,2,3
console.log(arr[index])//3,4,1
}
var obj={name:'wang',age:11};
for(var index in obj){
console.log(index);//name,age
console.log(obj(index);//wang,11
}
而of是直接获取值了。
再补充其他几种循环遍历的方法:

数组原生具备iterator接口(即默认部署了Symbol.iterator属性),for...of循环本质上就是调用这个接口产生的遍历器,可以用下面的代码证明。

const arr = ['red', 'green', 'blue'];

for(let v of arr) {
  console.log(v); // red green blue
}

const obj = {};
obj[Symbol.iterator] = arr[Symbol.iterator].bind(arr);

for(let v of obj) {
  console.log(v); // red green blue
}

上面代码中,空对象obj部署了数组arr的Symbol.iterator属性,结果obj的for...of循环,产生了与arr完全一样的结果。

for...of循环可以代替数组实例的forEach方法。

const arr = ['red', 'green', 'blue'];

arr.forEach(function (element, index) {
  console.log(element); // red green blue
  console.log(index);   // 0 1 2
});

JavaScript 原有的for...in循环,只能获得对象的键名,不能直接获取键值。ES6 提供for...of循环,允许遍历获得键值。

var arr = ['a', 'b', 'c', 'd'];

for (let a in arr) {
  console.log(a); // 0 1 2 3
}

for (let a of arr) {
  console.log(a); // a b c d
}

上面代码表明,for...in循环读取键名,for...of循环读取键值。如果要通过for...of循环,获取数组的索引,可以借助数组实例的entries方法和keys方法(参见《数组的扩展》一章)。

for...of循环调用遍历器接口,数组的遍历器接口只返回具有数字索引的属性。这一点跟for...in循环也不一样。

let arr = [3, 5, 7];
arr.foo = 'hello';

for (let i in arr) {
  console.log(i); // "0", "1", "2", "foo"
}

for (let i of arr) {
  console.log(i); //  "3", "5", "7"
}

上面代码中,for...of循环不会返回数组arr的foo属性。

参考“https://www.cnblogs.com/loveyoume/p/6112044.html”

转载于:https://www.cnblogs.com/sweeeper/p/8681583.html

js遍历属性 以及 js prototype 和继承 NULL 博文链接:https://jinhailion.iteye.com/blog/1530966 立即下载

相关推荐

金蝶云星空初级实施认证考题

Q4.[单选题] (1.5分) 总账中的多账簿账表主要是基于什么进行查看的Q47.[多选题] (2.5分) 销售订单的收款计划页签勾选预收之后,预收控制环节可以控制哪些节点?

m0_51994557的博客 3121

ES6-10【super、4种遍历方式、原型、symbol遍历】

一.原型 (1).原型知识拓展 1.我们需要牢记两点 (1).__proto__ 和constructor属性是对象所独有的; (2).prototype属性是函数所独有的;因为函数也是一种对象,所以也拥有__proto__和constructor属性 2.__proto__属性的作用就是当访问一个对象的属性时,如果该对象内部不存在这个属性,,那么就会去它的__proto__属性所指向的那个对象(父对象)里找,直到找到__proto__属性的终点null,再往上找就相当于在null上取值,会报

Yesterday_Tomorrow的博客 528

低成本主从机械臂系统构建:6D位姿数据采集与VLA模型训练实战

本文详细介绍了如何构建低成本主从机械臂系统,实现高精度6D位姿数据采集与VLA模型训练。通过300美元级别的硬件组合,系统达到±1mm位置精度和≤0.5°姿态误差,满足Vision-Language-Action模型的多模态训练需求。文章涵盖硬件选型、数据采集技巧、模型训练实战经验,为机器人研究提供经济高效的解决方案。

r7s8t的博客 266

你不知道的JavaScript--Item17 循环与prototype最后的几点小tips_循环,只循环新增属性,不循环prototype

在使用for…in循环时,不要依赖于遍历的顺序。当使用Object类型来保存数据时,需要保证其中的数据是无序的。当需要表示带有顺序的集合时,使用数组类型而不是Object类型。避免向Object.prototype中添加任何属性。如果确实有必要向Object.prototype中添加方法属性,可以考虑使用独立函数替代。使用Object.defineProperty来添加可以不被for…in循环遍历到的属性。当遍历数组时,使用标准的for循环,而不要使用for…in循环。

2401_89285746的博客 950

STGCN实战:从零构建PyTorch时空图卷积网络预测城市交通流

本文详细介绍了如何使用PyTorch从零构建时空图卷积网络(STGCN)进行城市交通流预测。通过将交通网络建模为图结构,并融合时间卷积与空间图卷积,STGCN能有效捕捉交通流的时空依赖性。文章提供了从数据预处理、核心模块(TimeBlock与STGCNBlock)搭建、模型训练到实战技巧的完整指南,帮助开发者快速掌握这一前沿的交通速度预测技术。

wasm7browser的博客 249

避坑指南:合宙Air780EG阿里云一型一密连接时,为什么你的HmacMD5加密总失败?

本文针对合宙Air780EG模组连接阿里云物联网平台时,HmacMD5签名失败的常见问题提供深度解决方案。详细剖析了签名拼接格式、密钥使用、随机数处理及编码大小写等关键陷阱,并结合STM32嵌入式开发环境,给出了串口调试、分步验证及AT指令链路的实用排错技巧,帮助开发者顺利完成一型一密动态注册。

kiwi8的博客 440

一文总结 JavaScript 对象遍历的几种方式

循环时,返回的是所有能够通过对象访问的、可枚举的属性,既包括存在于实例中的属性,也包括存在于原型中的实例。用于获取对象自身所有的可枚举的属性值,但不包括原型中的属性,然后返回二维数组。每一个子数组由对象的属性名、属性值组成。用于获取对象自身所有的可枚举的属性值(不包括 Symbol 值作为名称的属性),但不包括原型中的属性,然后返回一个由。用于获取对象自身所有的可枚举的属性值,但不包括原型中的属性,然后返回一个由。用于获取对象自身所有的可枚举的属性值,但不包括原型中的属性,然后返回一个由。

轻编程的博客 1302

WSL 运行make提示/lib/modules/xxx/build: No such file or directory. Stop.错误解决办法

WSL运行make提示/lib//modules/xxx文件或目录不存在。 WSL2的内核是修改过的,无法使用 ubuntu上游的内核头文件和modules文件,因此,我们需要手动编译并安装一个版本。

weixin_45668903的博客 1万+

看一遍都懂的数组遍历~确定不试试?

一:数组遍历 Array.prototype.map:(主要为了其返回值) map() 方法创建一个新数组,其结果是该数组中的每个元素是调用一次提供的函数后的返回值。 const numList = [1,2,3] // 对数组中的每一项都乘以 3, 然后返回新的数组 const resultLis = numList.map(item=> item * 3) console.log('旧数组:',numList) console.log('新数组:',resultLis) ...

localhost 293

AWR1642+DCA1000EVM 不使用mmWave Studio软件采集原始ADC数据 Timeout问题汇总

DCA1000 EVM Timeout问题汇总

Jesy_Hsu的博客 1476

游戏客户端开发工程师部分笔试题解析

你可以选择任意连续K个宝石合并成一个新的宝石,新宝石保持在原队列的位置且它的能量值是这K个宝石能量值之和。给定一个幸运值N=0、一堆包含能量值的宝石,你的任务是找到将所有宝石合并成一个宝石的最大幸运值。2.在一个二维网格中,有一个起点S、一个终点E和一个必须经过的中间点M。C++ 的多态性正是通过基类指针或引用调用派生类覆盖的虚函数实现的,这是运行时多态的核心机制。光线追踪带来的抗锯齿效果是其渲染方法的自然结果,而不是一个独立的算法。宝石具体能量值:[10, 7, 2, 6, 5, 11, 8];

博客 3029

Object.prototype 和 for in loop 遍历对象的使用

Object.prototype 与 for in 循环的搭配使用

weixin_39512883的博客 458

LangGraph 中 Human-in-the-loop

摘要:本文探讨了在自主AI Agent系统中实现"人工介入关键节点"(HIL)的技术方案,主要基于LangGraph框架构建。针对Agent可能执行高风险操作(如删除数据库)的问题,提出通过断点机制在关键节点暂停流程,实现人工审批后再继续执行。具体实现包括:(1)使用checkpointer组件持久化状态;(2)通过interrupt_before/after参数设置断点;(3)支持三种交互模式:批准、编辑和输入。文章还提供了代码示例,展示如何在不同场景(如工具调用)中动态触发人工审核机

qq_52053775的博客 153

Spring 循环引用 ——理解singleton与prototype初始化的区别

spring的循环引用解决机制 以及singleton与prototype能否实现循环引用的原因。

惟愿无事 1万+

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

Spring三级缓存解决循环依赖

1. 前言 循环依赖:就是N个类循环(嵌套)引用。 通俗的讲就是N个Bean互相引用对方,最终形成闭环。用一副经典的图示可以表示成这样(A、B、C都代表对象,虚线代表引用关系): 其实可以N=1,也就是极限情况的循环依赖:自己依赖自己 这里指的循环引用不是方法之间的循环调用,而是对象的相互依赖关系。(方法之间循环调用若有出口也是能够正常work的) 2. Spring Bean的...

傅红雪的专栏 2万+

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

spring的scope为prototype的bean的正确使用方法

spring的scope为prototype的bean的正确使用方法

Jaemon 4803

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

理解Spring循环引用(循环依赖)

test

轻松囧的专栏 1万+

prototype和__proto__的区别&&constructor 与 prototype 为何形成循环嵌套

1.Javascript中所有的对象都是Object的实例,并继承Object.prototype的属性和方法,也就是说,Object.prototype是最底层。 prototype是函数才有的属性 __proto__是每个对象都有的属性 prototype 就是一个对象,所以它有 __proto__ 属性,__proto__ 同时又指向 constructor 下的 prototype ,从...

chWebArmy的博客 264

JavaScript中几个重要的属性(this、constructor、prototype)

this  this表示当前对象,如果在全局作用范围内使用this,则指代当前页面对象window; 如果在函数中使用this,则this指代什么是根据运行时此函数在什么对象上被调用。 我们还可以使用apply和call两个全局方法来改变函数中this的具体指向。  先看一个在全局作用范围内使用this的例子:  console.log( === win

一个执着的程序员 2122

_proto_与prototype的关系及区别

__proto__与prototype这两个原型属性大概在学习前端的过程中多多少少都接触过,但是不经常用基本就忘了,不过面试的时候经常会被问到,然后就一脸懵逼的蒙混过关,总觉得这样不太好,所以看了些文章,觉得这位大神写的非常不错,简单易懂,贴给大家看看,共同学习! 原型是javascript面向对象编程中非常重要的概念,而且并不是那么容易懂。偶然看到一个题目:阐述proto和prototype的关系。看到这个问题的时候,我的脑海浮现出一些概念,但却说不出来。先来看一张图 如果能看懂图中的关系基本上就可以解

jijunqing321的博客 5457

你不知道的JavaScript--Item17 循环与prototype最后的几点小tips

1、优先使用数组而不是Object类型来表示有顺序的集合ECMAScript标准并没有规定对JavaScript的Object类型中的属性的存储顺序。但是在使用for..in循环对Object中的属性进行遍历的时候,确实是需要依赖于某种顺序的。正因为ECMAScript没有对这个顺序进行明确地规范,所以每个JavaScript执行引擎都能够根据自身的特点进行实现,那么在不同的执行环境中就不能保证fo

小平果的欢乐谷 1992

[[Prototype]]--浅析

[[Prototype]]JavaScript中的对象有一个特殊的[[Prototype]]内置属性,其实就是对于其他对象的引用。几乎所有的对象在创建时[[Prototype]]属性都会被赋予一个而非空的值。

林雨静 2936

Spring 如何解决循环依赖

1. 前言 循环依赖:就是N个类循环(嵌套)引用。 通俗的讲就是N个Bean互相引用对方,最终形成闭环。用一副经典的图示可以表示成这样(A、B、C都代表对象,虚线代表引用关系): ​ 其实可以N=1,也就是极限情况的循环依赖:自己依赖自己 这里指的循环引用不是方法之间的循环调用,而是对象的相互依赖关系。(方法之间循环调用若有出口也是能够正常work的) 2. Spring Bean的循环依赖 谈到Spring Bean的循环依赖,有的小伙伴可能比较陌生,毕竟开发过程中好像对循环依赖这个概念

天空中不留下鸟的痕迹,但我已经飞过 1340

重学ES系列之新增的几个循环方法

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>新增的几个循环方法</title> </head> <body> <script>

主要领域:前端开发 776
上一篇: 字符序列(characts)
下一篇: SELinux
weixin_30793643
博客等级 码龄11年 124粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值