<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
/*
需求:想把getMax与searchEle方法添加 到数组对象中。 functoin Array(){
this.prototype = new Object(); this.getMax = function(){ }
}
Prototype注意的细节:
1. prototype是函数(function)的一个必备属性(书面一点的说法是"保留属性")(只要是function,就一定有一个prototype属性)
2. prototype的值是一个对象
3. 可以任意修改函数的prototype属性的值。
4. 一个对象会自动拥有prototype的所有成员属性和方法。 */ Array.prototype.getMax = function(){
var max = this[0];
for(var index = 1; index<this.length ; index++){
if(this[index]>max){
max = this[index];
}
}
return max;
}
Array.prototype.searchEle = function(target){
for(var i = 0 ; i<this.length ; i++){
if(target==this[i]){
return i;
}
}
return -1; }
//var arr = new Array(12,4,17,9);
var arr = [12,4,17,9];
var max = arr.getMax();
var index = arr.searchEle(9);
document.write("最大值:"+ max+"<br/>");
document.write("索引值:"+ index+"<br/>"); </script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head> <body>
</body>
</html>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
/*
练习: 给字符串对象添加一个toCharArray的方法,然后再添加一个reverse(翻转)的 方法
*/ //把 字符串转换成字符数组
String.prototype.toCharArray = function(){
var arr = new Array();
for(var index = 0; index<this.length ;index++){
arr[index] = this.charAt(index);
}
return arr;
} String.prototype.reverse = function(){
//想把字符串转换成字符数组
var arr = this.toCharArray();
arr.reverse();
return arr.join("");
} var str = "你们厉害啊";
var charArr = str.toCharArray();
document.write("数组的元素:"+charArr.join(",")); str = str.reverse();
document.write("<br/>翻转后的字符串:"+str); </script> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head> <body>
</body>
</html>

输出:

数组的元素:你,们,厉,害,啊
翻转后的字符串:啊害厉们你

javascript之Prototype属性的更多相关文章

  1. Javascript中prototype属性详解 (存)

    Javascript中prototype属性详解   在典型的面向对象的语言中,如java,都存在类(class)的概念,类就是对象的模板,对象就是类的实例.但是在Javascript语言体系中,是不 ...

  2. (转载)详解Javascript中prototype属性(推荐)

    在典型的面向对象的语言中,如java,都存在类(class)的概念,类就是对象的模板,对象就是类的实例.但是在Javascript语言体系中,是不存在类(Class)的概念的,javascript中不 ...

  3. Javascript中prototype属性详解

    在典型的面向对象的语言中,如java,都存在类(class)的概念,类就是对象的模板,对象就是类的实例.但是在Javascript语言体系中,是不存在类(Class)的概念的,javascript中不 ...

  4. Javascript中prototype属性的详解

    原文链接:http://www.cnblogs.com/Uncle-Keith/p/5834289.html 在典型的面向对象的语言中,如java,都存在类(class)的概念,类就是对象的模板,对象 ...

  5. JavaScript 构造函数 prototype属性和_proto_和原型链 constructor属性 apply(),call()和bind() 关键字this

    1.构造函数: 通常构造函数首字母需要大写,主要是为了区别ECMAScript的其它函数.(高程三 P145) 构造函数与其他函数的唯一区别,就在于调用它们的方式不同.只要通过new来调用,任何函数都 ...

  6. Javascript中prototype属性

    prototype作为JS相对比较难理解的一个知识点,在这里发表下自己的理解. 本文将包含以下几部分内容: 1.js prototype的简单介绍, 2.js构造函数的介绍, 3.prototype的 ...

  7. 详解Javascript中prototype属性(推荐)

    在典型的面向对象的语言中,如java,都存在类(class)的概念,类就是对象的模板,对象就是类的实例.但是在Javascript语言体系中,是不存在类(Class)的概念的,javascript中不 ...

  8. 详解Javascript中prototype属性

    转自:https://www.jb51.net/article/91826.htm 在典型的面向对象的语言中,如java,都存在类(class)的概念,类就是对象的模板,对象就是类的实例.但是在Jav ...

  9. JavaScript和prototype

    Protoype这个词在javascript中可以有两种理解: 第一种是作为javascript中的一个属性,其一般出现的形式为:类名.prototype. prototype 属性让你有能力向对象添 ...

随机推荐

  1. layDate面板出现红色花纹图案

    要使用layDate,有两种方法: 1. 要么在引用layui.js和layui.css,然后通过layui.use('laydate', callback) 加载模块后,调用方法使用. 2. 去la ...

  2. OpenResty 执行流程阶段

    nginx有11个处理阶段,如下图所示: 指令 所处处理阶段 使用范围 解释 init_by_luainit_by_lua_file loading-config http nginx Master进 ...

  3. goquery 解析不了noscript

    今天在用goquery的时候 解析noscript标签的时候.发现一直获取不到里面的元素. google得到.需要去除noscript标签. s.Find("noscript"). ...

  4. Windows bat脚本的for语句

    Windows bat脚本的for语句基本形态如下: 在cmd窗口中:for %I in (command1) do command2 在批处理文件中:for %%I in (command1) do ...

  5. yii\base\InvalidCallException The cookie collection is read only.

    Invalid Call – yii\base\InvalidCallException The cookie collection is read only. 在使用Yii2进行cookie操作时会 ...

  6. AMD 异步模块定义

    1.解决模块加载过慢 2.核心:define函数

  7. SQLite3的安装与使用

    下载地址:https://www.sqlite.org/download.html (下载相对应自已电脑的配置的数据库)(这里 我的电脑是 windows 64位操作系统) 下载完后 解压出来 sql ...

  8. return new Promise的时候,不能带着.then()方法

    app.js return new Promise的同时带着.then()方法会出错 return出去的这个Promise,整体状态会显示pending,虽然详细里状态显示resolve,但是没有re ...

  9. npm报错 This is probably not a problem with npm,there is likely additional logging output above可能的原因

    npm WARN Local package.json exists, but node_modules missing, did you mean to install? 解决方法: 输入npm i ...

  10. Codevs 4373 窗口(线段树 单调队列 st表)

    4373 窗口 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题目描述 Description 给你一个长度为N的数组,一个长为K的滑动的窗体从最左移至最右端,你只 ...