js特点就是特灵活
我是分阶段敲得,可能会有注释的不合理问题,不过每一块代码去掉注释都可以运行
var tg=document.getElementById("ta");
tg.innerHTML="还行吧";
tg.style.backgroundColor="#aab";
var a="3.145";
var b=a-2;//自动转数值
var c=a+2;//字符串拼接
alert(b+"\n"+c)
c=parseInt(a)+2 3.999+2=5
c=parseFloat(a)+2 3.145+2=5.145
a=345t44 b=w222
parseInt(a) 345,//开头可以读,到非数停
parseInt(b) NaN,//开头就裂开
//全局范围,无论有无var都是全局变量,函数内局部,没用var,也是全局的;
//局部变量,函数里还要var,
var b="全局变量";
function myFun(){
age=4
var isMale=20;
}
myFun();//必须要调用才能拿
alert(b+"\n"+age);
alert(isMale);
var b="全局变量";
function myFun(){
var b="局部变量";//局部覆盖全局便梁,
b="局部变量"//给全局变量赋值,函数外也改变
alert(b);
}
myFun();//必须要调用才能拿
alert(b);
function test(o){
var i=0;
//typeof//判断类型是不是object
if(typeof o=="object"){
var j=5;
for(var k=0;k<10;k++){
//js没有代码块概念,作用范围是整个的函数内,不限于当前内函数
//只认函数体的{}
document.write(k);
}
alert(k+"\n"+j);
}
}
test(document);
var scope="全局变量";
function test(){
//变量提升,局部变量会在函数开始定义,所以西面undifine
if(0){var scope="局部遍历"; }
return ;
//变量提升定义,剋运行也会提升,不用赋值
document.write(scope);
var scope="局部遍历";
document.write(scope);
}
test();
var scope1="全局变量1";
var scope2="全局变量2";
var scope3="全局变量3";
alert(window.scope)//默认在window对象的属性;
//alert(window["scope"])//可点出属性,可以数组
for(var i=1;i<4;i++){
alert("scope"+1);//这只能输出变量名scope1,想输出值
alert(window["scope"+i]);
}
// let有块的范围。
function test(){
console.log(sc);
let sc="jibu";//报错你在le前不让你用
var sc="ww";//顶都是上面undifine
}
/*
ox 十六进制
0 开头八进制
0/0 = NaN
3/0 = Infinity,
-3/0 = -Infinity
undifined是创建的,没有赋值,null是有值,是为空
null==undifined; true值
null===undifined ; false类型不同;
var x,y=null
x==y true
x===y false
不存在的属性是undifiend,报错的话,大多是变量名不对应
const常量,final
NaN!=NaN;要用isNaN(a)
parseInt(a);得到的不是NaN就是数,所以再isNaN(a)
float 精度丢失;要用差值比较
字符和字符串一样var a="a"
charAt(1);获取索引处字符
a.length;
toUpperCase();大写
String.fromCharCode(93,94,95);找到unicode码对应的abc
indexOf("a",3);返回第一次出现位置,从3开始找
*/
var a="abc中国";
var b=a.length;
var c=String.fromCharCode(97,98,99);
alert(b+"-------"+a.charAt(4)+"-----"+a.charCodeAt(4)+"-----"+c)
var a="hellojavascript";
var b=a.indexOf("llo");
//从第几个开始找经常indexOf("llo",indexOf("a"));
//没找到返回-1;
var c=a.indexOf("llo",3);
var d=a.lastIndexOf("a");
alert(b+"\n"+c+"\n"+d+"\n")
subString()不可以负数
slice可以,slice(0,4)就是1234,
var s="abcdefg";
a=s.slice(0,4);
b=s.slice(2,4);
c=s.slice(4);
d=s.slice(3,-1);
e=s.slice(3,-2);
f=s.slice(-3,-1);
alert("a:"+a+"\nb :"
+b+"\nc :"
+c+"\nd :"
+d+"\ne :"
+e+"\nf :"
+f
)
match();
var s="abfd--abc@d.comcdefg";
search找到匹配到的位置
a=s.search(/[a-z]+@d.[a-zA-z]{2}m/);
var str="1dfd2dfs3df5";
match找到匹配到的东西,
var b=str.match(/\d/g);//g是全局找
alert(a+"\n"+b);
replace
结果:4 1,2,3,4
. /d /s /w ^ $
{2,5} 出现2-5次
? 0-1次
+ 1次以上
* 0到所有次
((k)|(l)) |是或者选择
trim()去掉开头结尾空格
test() /^[0-6]{2}$/.test("34")
alert(/^<a href=(\'|\")[a-zA-Z0-9\/:\.]*(\'|\")>.*<\/a>$/
.test("<a href='http://www.crezyit.org'>疯狂Java联盟</a>"));
function trim(s){
return s.replace(/(^\s*)|(\s*$)/g,"")
}
alert (trim(' hallo,J avaScript '));
var a=/^[0-6]{2}$/;//直面量
var a=new RegExp("[0-6]{2}");//面向对象
alert(a.test("34"))
数组赋值跳过不写 c[0]=1,c[4]=3,中间123赋值为空,c[5]undifined
长度可变,不会越界,没赋值的是undifined不会报错。
push();尾部
pop()
unshift();前端
shift();
var stack=[];
stack.push("悟空");
stack.push("八戒");
stack.push("白骨精");
console.log(stack.pop());
console.log(stack.pop());
var queue=[];
queue.unshift("疯狂讲义");
queue.unshift("疯狂讲义应用实战");
queue.unshift("疯狂讲义开发讲义");;
console.log(queue.shift());
console.log(queue.shift());
concat 新的数组,执行后就没了
var a=["html",2,"yeeku"];
console.log(a.concat(4,5));
console.log(a.concat([4,5]));
console.log(a.concat([4,5],[6,7]));
//join数组连起来join()里元素之间加的东西
var b=["html",20,"is",99,"good"];
console.log(b.join());
console.log(b.join("+"));
//reverse反转
var c=["html","css","jquary","bootstrap"];
c.reverse();
console.log(c);
//slice(),一个到最后,取字符;原数组不变,和下面splice反着
var d=["yeetu","leegeng","crazy","fkit","chaarlie"];
console.log(d.slice(3));
console.log(d.slice(2,4));
console.log(d.slice(1,-2));
console.log(d.slice(-3,-2));
//splice截取完后会导致原数组改变
var e=["yeetu","leegeng","crazy","fkit","chaarlie"];
console.log(e.splice(3));
console.log(e.splice(1,1));
console.log(e.splice(0,1,20,30,40));
console.log(e);
函数没重载,js只认函数名
函数常用if(typeof age==="number")
Math.pow(3,5);3^5次方
Math.sqrt(2);开根号
5=="5" true;"5"字符串转换int
5==="5" false 三等是类型等
!==要类型,值,全不等才可以
逗号运算符,只要最后的选择
a=(b=1,c=,
in2,d=3)
a=d=3;
void强制没返回值
a=void(b=1,c=2,d=3)
a=undifined;
null 类型是object
typeof 只知道类型 instanceof 判断是那个指定类,更深入
typeof dog=object//typeof是运算符
typeof (man)=object//typeof是函数
dog instanceof Dog
try{
for(var i=0;i<10;i++){
document.writeln(i+'<br />');
if(i<4)throw new Error("用户自定义错误");
//throw "我是错",下面console.log(e);
}
}catch(e){
console.log(e.massage);//接的是new的Error;
console.log(e);//接的是直接的;
document.writeln(e.massage);
}finally{
console.log("woquanyun");
}
js的异常只有一个Error定义函数时不用跑出,所以没有throws关键字
js是弱类型语言,不用声明类型;
try只有一个catch块,e.massage java用的是getMassage()
with(){
}
var s='C';
switch(s){
case 'A' : document.writeln("优秀.");
break;
case 'B' : document.writeln("良好.");
break;
case 'C':document.writeln("中");
break;
case 'D':document.writeln("几个");
break;
case 'E':document.writeln("不及格");
break;
default:document.writeln("裂开");
}
document.getElementById要让外部js defer,h后续执行
可是document.write就不行了;,htlml执行完,document就是固定了;
var a=["hallo","javascript","world"];
for(str in a){
document.writeln('索引'+str+'的值是'+a[str]+"<br/>");
}
for(var i=0;i<a.length;i++){
document.writeln('索引'+i+'的值是'+a[i]+"<br/>");
}
for( int a in b)
记得下面用b[a]操作,b.a大概率没这属性
document.writeln("<h1>navigator对象的全部属性:</h1>");
for(pro in navigator){
document.write('属性'+pro+'的值是'+navigator[pro]);
document.write("<br/>")
}
for(pro in navigator){
//a.b和
document.write('属性'+pro+'的值是'+navigator.pro);
document.write("<br/>")
}
break,结束整个for函数体
continue ,结束当前循环,当前for循环继续
正常名字加: ,就可以做标签,相当于坐标,在那结束就加哪
outer:
for(var i=0;i<5;i++){
for(var j=0;j<5;j++){
document.write('j的值是'+i);
//outer标签,结束外层,也就是全没了
if(j>=2) break outer;
document.writeln("i的值是:"+i);
document.writeln("<br/>")
}
}
ou:
for(var i=0;i<5;i++){
for(var j=0;j<5;j++){
document.write('j的值是'+i);
if(j>=2) continue ou;
document.writeln("i的值是:"+i);
document.writeln("<br/>")
}
}
函数提升,只能是在一个script标签里,就是先使用,在定义
匿名函数,若是后方有名字,如下面的test,test失效
var f=function test(name){
document.writeln('匿名<br/>');
document.writeln('你好,'+name)
};
f('yeeku');
由于函数就是类,并且是这个类的唯一构造器;
函数:
1. 函数可以被调用;
2. 定义一个函数,会创建一个对象,Function的实例;
3. 函数常赋给一个对象,匿名函数,
4. 定义时得到同名类,变量
这个同名变量很坑,如下
function test(name){
document.writeln('匿名<br/>');
document.writeln('你好,'+name)
}
test="ha";
test("en");
同名赋值给伴生的变量,test函数已经没了,成了普通变量
阶乘必须找对边界,逻辑规律
var fa=function(n){
if(typeof (n)=="number"){
if(n==1){
return 1;
}else{
return n*fa(n-1);
}
}else{
alert("类型不对");
}
}
alert(fa(5));
alert(fa(99));
function outer(){
function inner1(){
document.write("局部函数111<br/>");
}
function inner2(){
document.write("局部函数222<br/>");
}
document.write("开始测试局部函数。。。<br/>")
inner1();
inner2();
document.write("结束测试局部函数。。。<br/>")
}
document.write("调用outer之前。。。<br/>");
outer();
inner1();//不能运行,局部函数只存话在外部函数中
document.write("调用outer之后。。。<br/>");
var hallo=function(name){
return name+"您好";
}
alert("hello是否为Function的实例:"+(hallo instanceof Function)
+"\nhello是否为Object的实例:"+(hallo instanceof Object));
alert(hallo);//把函数作为字符串输出
alert(hallo("世界"));//把函数处理后的东西输出
function Person (name,age){
this.name=name;
this.age=age;
this.info=function(){
document.writeln("我的名字是:"+this.name+"<br/>");
document.writeln("我的年龄是:"+this.age);
}
}
可以直接调用,也可以new个新的
var p=new Person("meng",20);
p.info();
类里只有
function Person (na,age){
this.age=age;
Person.na=na;
var bb=0;
bb只能在类利用
}
函数三属性:局部,实例,类属性,都不可混用,混了,就是undifined
var p1=new Person("中国",29);
document.write("创建一个person对象<br/>");
document.write("p1的age属性:"+p1.age+"<br>");
document.write("p1的national属性:"+p1.na+"<br>");
document.write("通过Person类方式访问national的属性:"+Person.na+"<br>");
document.write("p1的bb属性:"+p1.bb+"<br>");
var p2=new Person("美国",32);
document.write("创建两个个person对象<br/>");
document.write("p2的age属性:"+p1.age+"<br>");
document.write("p2的national属性:"+p1.na+"<br>");
document.write("通过Person类方式访问national的属性:"+Person.na+"<br>");
document.write("p2的bb属性:"+p2.bb+"<br>");
var each=fuhnction(array,fn){
for(var index in array){
// fn.call(null,index,array[index]);
// fn.call(window,index,array[index]);
//fn(index,array[index])
//window.fn(index,array[index])不行,会认为是window的函数
}
}
each([4,20,3],fuhnction(index,ele){
document.write("第"+index+"个元素是:"+ele+"<br/>")
})
//独立体,谁吊我,this就是谁
function Person (name){
this.name=name;
this.info=function(){
alert("我的名字是:"+this.name);
}
}
var p=new Person ("yeeku");
p.info();
var name="测四名字";
//
p.info.call(window);
function Dog(name,age,bark){
this.name=name;
this.age=age;
this.bark=bark;
this.info=function(){
return this.name+"年龄:"+this.age+"它的叫声:"+this.bark;
}
}
var dog=new Dog("旺财",3,"汪汪汪。。。");
function Cat(name,age){
this.name=name;
this.age=age;
}
var cat=new Cat("kitty",2);
alert(dog.info.call(cat));
输出kitty年龄:2它的叫声:undefined
。call(cat),info是独立的,但是要先拿到dag.info虽然在Dog里
map,用数组接返回值,
forEach
var arr=["tee","fruit","lee","crazy"];
var newArr1 =arr.map(function(ele){
return ele.length;
});
var newArr2 =arr.map((ele)=>{
return ele.length;
});
var newArr3 =arr.map(ele =>ele.length);
console.log(newArr3);
arr.forEach(function(ele){
console.log(ele);
});
arr.forEach((ele)=>{
console.log(ele);
});
arr.forEach((ele)=>console.log(ele));
function Person(){
this.age=0;
setInterval(function growUp(){
console.log(this===window);
this.age++;
},1000);
}
var p=new Person();
setInterval(function(){
console.log(p.age);
},1000);
function Person(){
this.age=0;
setInterval(()=>{
console.log(this===window);
this.age++;
},1000);
}
var p=new Person();
setInterval(function(){
console.log(p.age);
},1000);
function fo(){
var f=function(i){return 'hallo,'+argument[i];
return f(2,7,9,97,0);
}
}
fo("a","b","c")//无用,没人接abc的值,argument隔着太远
也就是function(2) return f(2,7,9,97,0);的第一个值进去
argument[i]要的f(2,7,9,97,0);得植9
function fo(){
var f=(i)=>{return 'hallo,'+argument[i];
return f(2,7,9,97,0);
}
}
fo("a","b","c")
=> 没有this,和富函数通着,可以拿到 fo("a","b","c")
也就是function(2)
argument[i] 要的 fo("a","b","c") 输出 c
function changeAge(person){
person.age=10;
document.write("函数执行中person的age值:"+person.age+"<br/>");
person=null;
}
var person={age:5};
document.write("函数调用前person的age值:"+person.age+"<br/>");
changeAge(person);
document.write("函数调用后person的age值:"+person.age+"<br/>");
document.write("person对象:"+person);
functionPerson(){
var lo='crazy';
this.in=function(){
this.document.write("local:"+lo);
return lo;
}
var p=new personalbar();
var val =p.in();
alert(val);
}
function Person (name,age){
this.name=name;
this.age=age;
this.info=function(){
document.write("name:"+this.name+"<br>");
document.write("age:"+this.age+"<br>");
}
}
var p1=new Person('yee',29);
p1.info();
Person.prototype.walk=function(){
document.write(this.name+'溜达<br/>')
}
document.write('<hr/>');
var p2=new Person('lee',30);
p2.info();
document.write('<hr/>');
p1.walk();
p2.walk();
Array.prototype.indexOf=function(obj){
var result=-1;
for(var i=1;i<this.length;++i){
if(this[i]==obj){
result=i;
break;
}
}
return result;
}
var arr=[4,5,6,7];
alert ("数组[4,5,6,7]<br/>"+"找7<br>"+"位置:"+arr.indexOf(7));
去掉break
function Person (name,age){
this.name=name;
this.age=age;
}
Person.prototype.say=function(){
console.log(this.name+"打着招呼");
}
var per=new Person("牛魔王",22);
per.say();
function Stu(grade){
this.grade=grade;
}
Stu.prototype=new Person();
先继承在写自己的类
Stu.prototype.in=function(){
console.log("%s是个学生,读%d年纪",this.name,this.grade);
}
var stu=new Stu(5);
stu.name="悟空";
console.log(stu instanceof Stu);
console.log(stu instanceof Person);
stu.say();
stu.in();
function Person (name,age){
this.name=name;
this.age=age;
this.say=function(){
console.log(this.name+"打着招呼");
}
}
var per=new Person("牛魔王",22);
per.say();
function Stu(name,age,grade,){
Person.apply(this,[name,age]);
this.grade=grade;
}
//Stu.prototype=new Person();
Stu.prototype.in=function(){
console.log("%s是个学生,读%d年纪",this.name,this.grade);
}
var stu=new Stu("悟空",34,5);
console.log(stu instanceof Stu);
console.log(stu instanceof Person);
stu.say();
stu.in();
把函数拿出来Animal.protype.a=
原型链最强
先继承再写自己的,先写自己的会导致继承bie
Dog.prototype=new Animal();
Dog.prototype.say=function(){
console.log(this.type+"嗷嗷叫");
}
对象冒充,类内写的话函数在外面就没法获取了
function Stu(name,age,grade,){
Person.apply(this,[name,age]);
this.grade=grade;
}
function Animal(sex,age){
this.sex=sex;
this.age=age;
}
Animal.prototype.info=function(){
console.log("name:"+this.sex+"age:"+this.age);
}
function Dog(sex,age,type){
this.type=type;
animal.apply(this,[sex,age]);
}
//先继承再写自己的,先写自己的会导致继承bie
Dog.prototype=new Animal();
Dog.prototype.say=function(){
console.log(this.type+"嗷嗷叫");
}
var dog=new Dog("erha ",21,"哈士奇");
dog.info();
dog.say();
function Cat(sex,age,type){
this.type=type;
animal.apply(this,[sex,age]);
}
Cat.prototype=new Animal();
Cat.prototype.say=function(){
console.log(this.type+"喵喵叫");
}
var cat=new Cat("tom ",21,"波斯猫");
cat.info();
cat.say();
function Animal(sex,age){
this.sex=sex;
this.age=age;
}
Animal.prototype.info=function(){
console.log("name:"+this.sex+"age:"+this.age);
return this.age;
}
var cat=new Animal("tom ",21);
alert( cat.info());
function funA(){
var a = 10; // funA的活动对象之中;
return function(){ //匿名函数的活动对象;
alert(a);
}
}
var b = funA();
b(); //10
下面两个例子是区别全局,局部变量,在闭包作用下区别
function outerFn(){
var i = 0; //局部
function innerFn(){
i++;
console.log(i);
}
return innerFn;
}
var inner = outerFn(); //每次外部函数执行的时候,都会开辟一块内存空间,外部函数的地址不同,都会重新创建一个新的地址
inner();
inner();
inner();
var inner2 = outerFn();
inner2();
inner2();
inner2(); //1 2 3 1 2 3
var i = 0;、、全局
function outerFn(){
function innnerFn(){
i++;
console.log(i);
}
return innnerFn;
}
var inner1 = outerFn();
var inner2 = outerFn();
inner1();
inner2();
inner1();
inner2(); //1 2 3 4
本文深入探讨JavaScript的灵活性及其实用编程技巧,包括变量作用域、数据类型、字符串操作、正则表达式、数组方法、函数提升、闭包、原型链、继承模式等关键概念,同时提供丰富的代码示例。

被折叠的 条评论
为什么被折叠?



