gpt4 book ai didi

javascript - jQuery @变量?

转载 作者:行者123 更新时间:2023-12-01 02:24:05 24 4
gpt4 key购买 nike

有人在另一个网站上的代码出现了问题,我注意到他们的代码中大量使用了 @,我只是好奇它是什么以及他为什么使用它。

这是他的代码:

# check if supports css3
supports = (->
div = document.createElement("div")
vendors = "Khtml Ms O Moz Webkit".split(" ")
len = vendors.length
(prop) ->
return true if prop of div.style
prop = prop.replace(/^[a-z]/, (val) ->
val.toUpperCase()
)
while len--
return true if vendors[len] + prop of div.style
false
)()

class HandleCarousel
constructor: (wrapper, block_number) ->
@block_number = block_number
@wrapper = wrapper

wrapper = null
counter: 0
block_number: 0
loopTimeout = 5000
loopTimer = null

handleBlur: =>
helpers.windowState = 'blur'
window.clearTimeout(@loopTimer)
@counter = 0
# reset all
target = @wrapper.find('ul')
target.css({left: 0})
target.clearQueue()
target.find('ul').stop()
handleFocus: =>
if helpers.windowState == 'blur'
helpers.windowState = 'focus'
@start(5000)

handleWindowState: ->
$(window).blur(@handleBlur)
$(window).focus(@handleFocus)

loop: ->
@counter++
slider = @wrapper.find('ul')
block = @wrapper.find('ul li')
delta = parseInt(block.css('width'), 10) * @counter * (-1)
if (@counter < @block_number)
# check css3 support
if slider.hasClass 'css3'
slider.css({'left': delta})
else
slider.animate({'left': delta},{duration: 500, easing: 'easeInSine'})
else
@counter = 0
@wrapper.find('ul').css({left: 0})

if (@counter is 0 || @counter is @block_number-1)
if @counter is 0
slider.removeClass 'css3'
@loopTimeout = 2500
#/
else
slider.addClass 'css3'
@loopTimeout = 5000

@start(@loopTimeout) #if helpers.windowState isnt 'blur'

# start loop slider
start: (time) ->
console.log 'start'
@loopTimer = window.setTimeout( =>
@loop()
, time)



helpers = {
windowState: 'focus'
iDevice: /iphone|ipad/i.test(navigator.userAgent.toLowerCase())
updateOrientation: ->
location.reload()
hasTouch : /android|iphone|ipad/i.test(navigator.userAgent.toLowerCase())
supportsTouch: -> # "'createTouch' in document" will return true in Apple's Mobile Safari. Otherwise detect Android directly.
android = navigator.userAgent.indexOf('Android') != -1
return android || !!('createTouch' in document)

}# /helpers


jQuery ->
window.console ?=
log:-> #patch so console.log() never causes error even in IE.

if $('.carousel').length>0
target = $('.carousel')
if supports('textShadow')
target.find('ul').addClass 'css3'
firstImg = target.find('ul li:first').html()
console.log firstImg
target.find('ul').append('<li>'+firstImg+'</li>')
carousel_items = target.find('li').length
if carousel_items > 2
carousel = new HandleCarousel(target, carousel_items)
carousel.handleWindowState()
carousel.start(carousel.loopTimeout)

最佳答案

那不是 jQuery 或 JavaScript。是CoffeeScript最终会转换为 JavaScript。

@ 用于引用当前上下文的属性 (this)。所以 CoffeeScript 的,

@something

相当于 JavaScript 的

this.something

关于javascript - jQuery @变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7374104/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com