js基本语法和创建对象

js基本语法:

var reg1=/\d/;
var reg2=new RegExp("\d");
/*\d  0-9任意一个字符  字母小写 大写取反
 []   任意一个字符
 [0-9]  \d
 12=>[1][2]   12
 [12a]=>1、2、a
 [a-zA-Z0-9]
 [^] 非其中的任意一个字符
    [^0-9]
 \w  数字、字母、下划线
 .   任意一个字符
 [.]  .
  |   或
      2|3 2、 3
      1[0-2]
      1[012]
    ?  0-1次
       0?[1-9]
    +  1-多次
    +  0-多次
    {,}最少次数,最多次数
    {6,}最少6个
    {,12}最多12个
    ^  开始
    $  结束
*/
var txt="abc0";
var reg=/^\d+$/
console.log(reg.test(txt));//false

// 1[3-9]\d{9}匹配手机号
//  \d{4}-(0?[1-9]|[0-2])-(0?[1-9]|[12][0-9]|3[01]) 匹配年月份
// [\u4e00-u9fa5]
// [\u4e00\u4e00]  
 
创建对象:
// 字面量  json
var student={
    id:10001,
    name:"张三",
    scores:[
        {subject:"html",score:90},
        {subject:"JS",score:90}
    ]
}
// function
function Student(id,name){
    this.id=id;
    this.scores=[
        {subject:"html",score:90},
        {subject:"JS",score:90}
    ]
}
//扩展  向原有的内容添加内容
Student.prototype.sex="男";
Student.prototype.eat=function(food){
    console.log("吃"+food);
}
var stu=new Student(1000,"张三");
stu.eat("米饭");
console.log(stu.sex);

// object
var stu2=new Object();
stu2.id=10000;
stu2.name="张三";
stu2.scores=[
        {subject:"html",score:90},
        {subject:"JS",score:90}
]
//反复调用  return this
// 链式编程  可以在后面一直加".内容"
eg:
function Student(id,name){
    this.id=id;
    this.scores=[
        {subject:"html",score:90},
        {subject:"JS",score:90}
    ],
    this.eat=function(food){
        console.log("吃"+food)
        return this
    },
    this.sleep=function(){
        console.log("睡")
        return this
    }
}
var stu=new Student(1001,"张三");
stu.eat("").sleep().eat("").sleep().eat("").sleep();
 
 
 
 
 
 
 
 
 
 
 
 
posted @ 2021-11-04 20:34  与神明画过押  阅读(37)  评论(0)    收藏  举报