JavaScript中所有的事物都是对象。
自定义对象:定义并创建对象极其实例 ; 用函数定义对象,再创建对象实例
<span style="font-size:18px;"> <script>
people = new Object();
people.name = "小囧";
people.age = "18";
document.write(people.name+"今年"+people.age);
</script></span>
(小囧年年18)
String字符串对象
常用的字符串属性:length、prototype、constructor
常用的字符串方法:charAt()、charCodeAt()、concat()、fromCharCode()、indexOf()、lastIndexOf()、match()、replace()、search()、slice()、substring()、substr()、valueOf、toLowerCase()、toUpperCase()、split()
以下是常用的示例
<pre name="code" class="javascript"> <script>
var str = "Hello This Beautiful World";
document.write("字符串长度为"+str.length);
<script>
输出如下:
<pre name="code" class="javascript"> <script>
var str = "Hello This Beautiful World";
document.write(str.indexOf("World"));
<script>
输入如下:21 指到world这个字符时候的长度
<pre name="code" class="javascript"> <script>
<pre name="code" class="javascript"> var str = "Hello This Beautiful World";
document.write(str.match("Beautiful"));
<script>
输出如下: Beautiful 查找是否有Beautiful这个字符,如果有就输出,没有则输出null
<pre name="code" class="javascript"> <script>
var str = "Hello This Beautiful World";
document.write(str.replace("World","小囧"));
<script>
输出如下:
<script>
var str = "Hello This Beautiful World";
var i = str.split(" "); //通过“ ”作为分割线
document.write(i[0]); //查找第0个字符
</script>
输出如下: Hello